2020-04-24 20:59:11 +00:00
|
|
|
import 'dart:typed_data';
|
2020-04-24 17:34:46 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
2020-05-01 18:20:12 +00:00
|
|
|
import 'package:photos/core/image_cache.dart';
|
|
|
|
import 'package:photos/core/thumbnail_cache.dart';
|
|
|
|
import 'package:photos/models/photo.dart';
|
|
|
|
import 'package:photos/ui/loading_widget.dart';
|
2020-04-24 17:34:46 +00:00
|
|
|
import 'package:photo_view/photo_view.dart';
|
2020-05-01 18:20:12 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2020-04-24 17:34:46 +00:00
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
class ZoomableImage extends StatefulWidget {
|
|
|
|
final Photo photo;
|
2020-04-24 17:34:46 +00:00
|
|
|
final Function(bool) shouldDisableScroll;
|
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
ZoomableImage(
|
2020-04-24 17:34:46 +00:00
|
|
|
this.photo, {
|
|
|
|
Key key,
|
|
|
|
this.shouldDisableScroll,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
@override
|
|
|
|
_ZoomableImageState createState() => _ZoomableImageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ZoomableImageState extends State<ZoomableImage> {
|
|
|
|
ImageProvider _imageProvider;
|
2020-05-04 15:35:23 +00:00
|
|
|
bool _loadedSmallThumbnail = false;
|
|
|
|
bool _loadedLargeThumbnail = false;
|
2020-04-24 20:47:21 +00:00
|
|
|
bool _loadedFinalImage = false;
|
|
|
|
ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_scaleStateChangedCallback = (value) {
|
|
|
|
if (widget.shouldDisableScroll != null) {
|
|
|
|
widget.shouldDisableScroll(value != PhotoViewScaleState.initial);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
super.initState();
|
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-04 15:35:23 +00:00
|
|
|
if (!_loadedSmallThumbnail &&
|
|
|
|
!_loadedLargeThumbnail &&
|
|
|
|
!_loadedFinalImage) {
|
2020-04-25 10:28:22 +00:00
|
|
|
final cachedThumbnail =
|
2020-05-04 15:35:23 +00:00
|
|
|
ThumbnailLruCache.get(widget.photo, THUMBNAIL_SMALL_SIZE);
|
2020-04-24 20:47:21 +00:00
|
|
|
if (cachedThumbnail != null) {
|
|
|
|
_imageProvider = Image.memory(cachedThumbnail).image;
|
2020-05-04 15:35:23 +00:00
|
|
|
_loadedSmallThumbnail = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_loadedLargeThumbnail && !_loadedFinalImage) {
|
|
|
|
final cachedThumbnail =
|
|
|
|
ThumbnailLruCache.get(widget.photo, THUMBNAIL_LARGE_SIZE);
|
|
|
|
if (cachedThumbnail != null) {
|
|
|
|
_onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
|
|
|
|
} else {
|
|
|
|
widget.photo
|
|
|
|
.getAsset()
|
|
|
|
.thumbDataWithSize(THUMBNAIL_LARGE_SIZE, THUMBNAIL_LARGE_SIZE)
|
|
|
|
.then((data) {
|
|
|
|
_onLargeThumbnailLoaded(Image.memory(data).image, context);
|
|
|
|
ThumbnailLruCache.put(widget.photo, THUMBNAIL_LARGE_SIZE, data);
|
|
|
|
});
|
2020-04-24 20:47:21 +00:00
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
}
|
2020-04-24 20:47:21 +00:00
|
|
|
|
|
|
|
if (!_loadedFinalImage) {
|
2020-05-04 15:35:23 +00:00
|
|
|
final cachedImage = ImageLruCache.get(widget.photo);
|
|
|
|
if (cachedImage != null) {
|
|
|
|
_onFinalImageLoaded(cachedImage, context);
|
2020-04-24 20:59:11 +00:00
|
|
|
} else {
|
|
|
|
widget.photo.getBytes().then((bytes) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_onFinalImageLoaded(bytes, context);
|
2020-05-04 15:35:23 +00:00
|
|
|
ImageLruCache.put(widget.photo, bytes);
|
2020-04-24 20:47:21 +00:00
|
|
|
});
|
2020-04-24 20:59:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-24 20:47:21 +00:00
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
if (_imageProvider != null) {
|
2020-04-24 21:21:37 +00:00
|
|
|
return PhotoView(
|
2020-04-24 20:47:21 +00:00
|
|
|
imageProvider: _imageProvider,
|
|
|
|
scaleStateChangedCallback: _scaleStateChangedCallback,
|
|
|
|
minScale: PhotoViewComputedScale.contained,
|
|
|
|
gaplessPlayback: true,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return loadWidget;
|
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
}
|
2020-04-24 20:59:11 +00:00
|
|
|
|
2020-05-04 15:35:23 +00:00
|
|
|
void _onLargeThumbnailLoaded(
|
|
|
|
ImageProvider imageProvider, BuildContext context) {
|
|
|
|
precacheImage(imageProvider, context).then((value) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_imageProvider = imageProvider;
|
|
|
|
_loadedLargeThumbnail = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_loadedLargeThumbnail = true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 20:59:11 +00:00
|
|
|
void _onFinalImageLoaded(Uint8List bytes, BuildContext context) {
|
|
|
|
final imageProvider = Image.memory(bytes).image;
|
|
|
|
precacheImage(imageProvider, context).then((value) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_imageProvider = imageProvider;
|
|
|
|
_loadedFinalImage = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
}
|