2020-03-28 18:18:27 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-05-01 18:20:12 +00:00
|
|
|
import 'package:photos/core/thumbnail_cache.dart';
|
|
|
|
import 'package:photos/models/photo.dart';
|
|
|
|
import 'package:photos/core/constants.dart';
|
2020-03-28 18:18:27 +00:00
|
|
|
|
2020-04-25 09:12:13 +00:00
|
|
|
class ThumbnailWidget extends StatefulWidget {
|
2020-04-11 21:29:07 +00:00
|
|
|
final Photo photo;
|
2020-03-28 18:18:27 +00:00
|
|
|
|
2020-04-25 09:12:13 +00:00
|
|
|
const ThumbnailWidget(
|
2020-04-11 21:29:07 +00:00
|
|
|
this.photo, {
|
2020-03-28 18:18:27 +00:00
|
|
|
Key key,
|
|
|
|
}) : super(key: key);
|
|
|
|
@override
|
2020-04-25 09:12:13 +00:00
|
|
|
_ThumbnailWidgetState createState() => _ThumbnailWidgetState();
|
2020-03-28 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:12:13 +00:00
|
|
|
class _ThumbnailWidgetState extends State<ThumbnailWidget> {
|
2020-04-13 17:53:03 +00:00
|
|
|
static final Widget loadingWidget = Container(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
color: Colors.grey[500],
|
|
|
|
);
|
2020-04-25 09:12:13 +00:00
|
|
|
|
2020-05-04 14:08:26 +00:00
|
|
|
bool _hasLoadedThumbnail = false;
|
2020-04-25 10:28:22 +00:00
|
|
|
ImageProvider _imageProvider;
|
|
|
|
|
2020-03-28 18:18:27 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-04 14:08:26 +00:00
|
|
|
if (!_hasLoadedThumbnail) {
|
2020-04-25 10:28:22 +00:00
|
|
|
final cachedSmallThumbnail =
|
|
|
|
ThumbnailLruCache.get(widget.photo, THUMBNAIL_SMALL_SIZE);
|
|
|
|
if (cachedSmallThumbnail != null) {
|
2020-05-04 14:08:26 +00:00
|
|
|
final imageProvider = Image.memory(cachedSmallThumbnail).image;
|
|
|
|
precacheImage(imageProvider, context).then((value) {
|
2020-04-25 12:13:32 +00:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
2020-05-04 14:08:26 +00:00
|
|
|
_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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-04-25 12:13:32 +00:00
|
|
|
}
|
2020-05-04 14:08:26 +00:00
|
|
|
ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data);
|
2020-04-25 12:13:32 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-25 10:28:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 18:18:27 +00:00
|
|
|
|
2020-04-25 10:28:22 +00:00
|
|
|
if (_imageProvider != null) {
|
|
|
|
return Image(
|
2020-04-25 10:42:27 +00:00
|
|
|
image: _imageProvider,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
);
|
2020-04-25 10:28:22 +00:00
|
|
|
} else {
|
|
|
|
return loadingWidget;
|
|
|
|
}
|
2020-03-28 18:18:27 +00:00
|
|
|
}
|
2020-04-27 13:02:29 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(ThumbnailWidget oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
if (widget.photo.generatedId != oldWidget.photo.generatedId) {
|
|
|
|
setState(() {
|
2020-05-04 14:08:26 +00:00
|
|
|
_hasLoadedThumbnail = false;
|
2020-04-27 13:02:29 +00:00
|
|
|
_imageProvider = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 18:18:27 +00:00
|
|
|
}
|