Created
September 23, 2021 16:36
-
-
Save oyeolamilekan/847bddd798fa7a17cf7935707dfd255f to your computer and use it in GitHub Desktop.
Cached image provider, snippet.
This file contains hidden or 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
| class FETTCachedImageContainer extends StatefulWidget { | |
| final String title; | |
| final String? imageUrl; | |
| final VoidCallback downloadImage; | |
| const FETTCachedImageContainer({ | |
| Key? key, | |
| required this.title, | |
| required this.downloadImage, | |
| this.imageUrl, | |
| }) : super(key: key); | |
| @override | |
| _FETTCachedImageContainerState createState() => | |
| _FETTCachedImageContainerState(); | |
| } | |
| class _FETTCachedImageContainerState extends State<FETTCachedImageContainer> { | |
| ValueNotifier<int> _networklHasErrorNotifier = ValueNotifier(0); | |
| void _enlarge(String? imageUrl) { | |
| Get.dialog( | |
| GestureDetector( | |
| onTap: () { | |
| Get.back(); | |
| }, | |
| child: STEMPhotoView( | |
| title: widget.title, | |
| imageUrl: imageUrl, | |
| download: widget.downloadImage, | |
| image: CachedNetworkImageProvider(imageUrl as String), | |
| ), | |
| ), | |
| ); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Container( | |
| height: 300, | |
| width: 80.w, | |
| child: ValueListenableBuilder( | |
| valueListenable: _networklHasErrorNotifier, | |
| builder: (BuildContext context, int count, Widget? child) { | |
| return CachedNetworkImage( | |
| cacheKey: | |
| "${widget.imageUrl}-${_networklHasErrorNotifier.value.toString()}", | |
| imageUrl: widget.imageUrl!, | |
| imageBuilder: (context, imageProvider) => InkWell( | |
| onTap: () => _enlarge(widget.imageUrl), | |
| child: Container( | |
| alignment: Alignment.topLeft, | |
| decoration: BoxDecoration( | |
| image: DecorationImage( | |
| image: imageProvider, | |
| fit: BoxFit.cover, | |
| ), | |
| borderRadius: BorderRadius.circular(5), | |
| ), | |
| ), | |
| ), | |
| placeholder: (context, url) => Shimmer.fromColors( | |
| baseColor: Colors.grey[300]!, | |
| highlightColor: Colors.white, | |
| period: Duration(milliseconds: 500), | |
| child: Container( | |
| decoration: BoxDecoration( | |
| borderRadius: BorderRadius.circular( | |
| 5, | |
| ), | |
| color: Colors.grey, | |
| ), | |
| ), | |
| ), | |
| errorWidget: (context, url, error) => InkWell( | |
| onTap: () { | |
| _networklHasErrorNotifier.value++; | |
| STEM.successSnackBar("Re downloading image"); | |
| }, | |
| child: Container( | |
| width: 100.w, | |
| decoration: BoxDecoration( | |
| borderRadius: BorderRadius.circular( | |
| 5, | |
| ), | |
| color: Colors.grey, | |
| ), | |
| child: Icon(Icons.replay_outlined), | |
| ), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment