From 5bd8b9b0268fc83c5e3f4bd686572540c6cadd2d Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Mon, 4 May 2020 19:38:26 +0530 Subject: [PATCH] Refactor thumbnail widget --- lib/ui/thumbnail_widget.dart | 51 +++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/ui/thumbnail_widget.dart b/lib/ui/thumbnail_widget.dart index 44491b58f..f51097d6e 100644 --- a/lib/ui/thumbnail_widget.dart +++ b/lib/ui/thumbnail_widget.dart @@ -20,32 +20,49 @@ class _ThumbnailWidgetState extends State { color: Colors.grey[500], ); - bool _loadedSmallThumbnail = false; - bool _loadedLargeThumbnail = false; + bool _hasLoadedThumbnail = false; ImageProvider _imageProvider; @override Widget build(BuildContext context) { - if (!_loadedSmallThumbnail && !_loadedLargeThumbnail) { + if (!_hasLoadedThumbnail) { final cachedSmallThumbnail = ThumbnailLruCache.get(widget.photo, THUMBNAIL_SMALL_SIZE); if (cachedSmallThumbnail != null) { - _imageProvider = Image.memory(cachedSmallThumbnail).image; - _loadedSmallThumbnail = true; - } else { - widget.photo - .getAsset() - .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE) - .then((data) { + final imageProvider = Image.memory(cachedSmallThumbnail).image; + precacheImage(imageProvider, context).then((value) { if (mounted) { setState(() { - if (data != null) { - _imageProvider = Image.memory(data).image; - } - _loadedSmallThumbnail = true; + _imageProvider = imageProvider; + _hasLoadedThumbnail = true; + }); + } + }); + } else { + Future.delayed(Duration(milliseconds: 1000), () { + if (mounted) { + widget.photo + .getAsset() + .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE) + .then((data) { + if (mounted) { + setState(() { + if (data != null) { + final imageProvider = Image.memory(data).image; + precacheImage(imageProvider, context).then((value) { + if (mounted) { + setState(() { + _imageProvider = imageProvider; + _hasLoadedThumbnail = true; + }); + } + }); + } + }); + } + ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data); }); } - ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data); }); } } @@ -53,7 +70,6 @@ class _ThumbnailWidgetState extends State { if (_imageProvider != null) { return Image( image: _imageProvider, - gaplessPlayback: true, fit: BoxFit.cover, ); } else { @@ -66,8 +82,7 @@ class _ThumbnailWidgetState extends State { super.didUpdateWidget(oldWidget); if (widget.photo.generatedId != oldWidget.photo.generatedId) { setState(() { - _loadedSmallThumbnail = false; - _loadedLargeThumbnail = false; + _hasLoadedThumbnail = false; _imageProvider = null; }); }