Created
April 3, 2024 07:22
-
-
Save ashutoshpw/6f59a3071a91a3f3cfb20deaa1b0cb37 to your computer and use it in GitHub Desktop.
image_loader.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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