Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ashutoshpw/6f59a3071a91a3f3cfb20deaa1b0cb37 to your computer and use it in GitHub Desktop.
Save ashutoshpw/6f59a3071a91a3f3cfb20deaa1b0cb37 to your computer and use it in GitHub Desktop.
image_loader.dart
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'dart:io';
class CachedNetworkSvg extends StatelessWidget {
final String url;
final String semanticsLabel;
const CachedNetworkSvg({
Key? key,
required this.url,
this.semanticsLabel = '',
}) : super(key: key);
Future<File> _loadAndCacheSvg(String url) async {
final file = await DefaultCacheManager().getSingleFile(url);
return file;
}
@override
Widget build(BuildContext context) {
return FutureBuilder<File>(
future: _loadAndCacheSvg(url),
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
// SVG is loaded, display it
return SvgPicture.file(
snapshot.data!,
semanticsLabel: semanticsLabel,
);
} else if (snapshot.hasError) {
// Handle errors, e.g., display an error icon
return Icon(Icons.error);
} else {
// Display a placeholder while loading
return Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
);
}
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment