Refactor thumbnail widget

This commit is contained in:
Vishnu Mohandas 2020-05-04 19:38:26 +05:30
parent 98f413fbb3
commit 5bd8b9b026

View file

@ -20,19 +20,27 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
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;
final imageProvider = Image.memory(cachedSmallThumbnail).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
});
} else {
Future.delayed(Duration(milliseconds: 1000), () {
if (mounted) {
widget.photo
.getAsset()
.thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
@ -40,20 +48,28 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
if (mounted) {
setState(() {
if (data != null) {
_imageProvider = Image.memory(data).image;
final imageProvider = Image.memory(data).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
});
}
_loadedSmallThumbnail = true;
});
}
ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data);
});
}
});
}
}
if (_imageProvider != null) {
return Image(
image: _imageProvider,
gaplessPlayback: true,
fit: BoxFit.cover,
);
} else {
@ -66,8 +82,7 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
super.didUpdateWidget(oldWidget);
if (widget.photo.generatedId != oldWidget.photo.generatedId) {
setState(() {
_loadedSmallThumbnail = false;
_loadedLargeThumbnail = false;
_hasLoadedThumbnail = false;
_imageProvider = null;
});
}