ente/lib/ui/zoomable_image.dart

194 lines
6 KiB
Dart
Raw Normal View History

2020-06-10 18:17:54 +00:00
import 'package:extended_image/extended_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
2020-05-04 20:44:34 +00:00
import 'package:photos/core/cache/image_cache.dart';
import 'package:photos/core/cache/thumbnail_cache.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/models/photo.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photo_view/photo_view.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/core/constants.dart';
class ZoomableImage extends StatefulWidget {
final Photo photo;
final Function(bool) shouldDisableScroll;
ZoomableImage(
this.photo, {
Key key,
this.shouldDisableScroll,
}) : super(key: key);
@override
_ZoomableImageState createState() => _ZoomableImageState();
}
2020-06-10 18:17:54 +00:00
class _ZoomableImageState extends State<ZoomableImage>
with SingleTickerProviderStateMixin {
ImageProvider _imageProvider;
2020-05-04 15:35:23 +00:00
bool _loadedSmallThumbnail = false;
2020-06-10 00:04:22 +00:00
bool _loadingLargeThumbnail = false;
2020-05-04 15:35:23 +00:00
bool _loadedLargeThumbnail = false;
2020-06-10 00:04:22 +00:00
bool _loadingFinalImage = false;
bool _loadedFinalImage = false;
ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
2020-06-10 18:17:54 +00:00
// AnimationController _animationController;
// Animation _animation;
// VoidCallback _animationListener;
final doubleTapScales = [1.0, 2.0];
@override
void initState() {
_scaleStateChangedCallback = (value) {
if (widget.shouldDisableScroll != null) {
widget.shouldDisableScroll(value != PhotoViewScaleState.initial);
}
};
2020-06-10 18:17:54 +00:00
// _animationController = AnimationController(
// vsync: this,
// duration: const Duration(milliseconds: 100),
// );
super.initState();
}
@override
Widget build(BuildContext context) {
2020-05-25 14:54:54 +00:00
if (widget.photo.localId == null) {
_loadNetworkImage();
} else {
_loadLocalImage(context);
}
if (_imageProvider != null) {
2020-06-10 18:17:54 +00:00
// return ExtendedImage(
// image: _imageProvider,
// gaplessPlayback: true,
// mode: ExtendedImageMode.gesture,
// enableSlideOutPage: true,
// initGestureConfigHandler: (state) {
// return GestureConfig(
// inPageView: true,
// initialScale: 1.0,
// minScale: 1.0,
// );
// },
// onDoubleTap: (ExtendedImageGestureState state) {
// var pointerDownPosition = state.pointerDownPosition;
// double begin = state.gestureDetails.totalScale;
// double end;
// _animation?.removeListener(_animationListener);
// _animationController.stop();
// _animationController.reset();
// if (begin == doubleTapScales[0]) {
// end = doubleTapScales[1];
// } else {
// end = doubleTapScales[0];
// }
// _animationListener = () {
// state.handleDoubleTap(
// scale: _animation.value,
// doubleTapPosition: pointerDownPosition);
// };
// _animation =
// _animationController.drive(Tween<double>(begin: begin, end: end));
// _animation.addListener(_animationListener);
// _animationController.forward();
// },
// );
2020-05-25 14:54:54 +00:00
return PhotoView(
imageProvider: _imageProvider,
scaleStateChangedCallback: _scaleStateChangedCallback,
minScale: PhotoViewComputedScale.contained,
gaplessPlayback: true,
);
} else {
return loadWidget;
}
}
void _loadNetworkImage() {
if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) {
_imageProvider = Image.network(widget.photo.getThumbnailUrl()).image;
_loadedSmallThumbnail = true;
}
if (!_loadedFinalImage) {
widget.photo.getBytes().then((data) {
_onFinalImageLoaded(Image.memory(data).image, context);
});
}
2020-05-25 14:54:54 +00:00
}
void _loadLocalImage(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);
if (cachedThumbnail != null) {
_imageProvider = Image.memory(cachedThumbnail).image;
2020-05-04 15:35:23 +00:00
_loadedSmallThumbnail = true;
}
}
2020-06-10 00:04:22 +00:00
if (!_loadingLargeThumbnail &&
!_loadedLargeThumbnail &&
!_loadedFinalImage) {
_loadingLargeThumbnail = true;
2020-05-04 15:35:23 +00:00
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-06-10 00:04:22 +00:00
if (!_loadingFinalImage && !_loadedFinalImage) {
_loadingFinalImage = true;
2020-05-05 20:58:43 +00:00
final cachedFile = ImageLruCache.get(widget.photo);
if (cachedFile != null) {
final imageProvider = Image.file(cachedFile).image;
_onFinalImageLoaded(imageProvider, context);
2020-04-24 20:59:11 +00:00
} else {
widget.photo.getAsset().file.then((file) {
2020-04-24 20:59:11 +00:00
if (mounted) {
2020-06-10 00:04:22 +00:00
final imageProvider = Image.file(file).image;
_onFinalImageLoaded(imageProvider, context);
ImageLruCache.put(widget.photo, file);
2020-04-24 20:59:11 +00:00
}
});
}
}
}
2020-04-24 20:59:11 +00:00
2020-05-04 15:35:23 +00:00
void _onLargeThumbnailLoaded(
ImageProvider imageProvider, BuildContext context) {
2020-06-10 00:04:22 +00:00
if (!_loadedFinalImage) {
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedLargeThumbnail = true;
});
}
});
}
2020-05-04 15:35:23 +00:00
}
void _onFinalImageLoaded(ImageProvider imageProvider, BuildContext context) {
2020-04-24 20:59:11 +00:00
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedFinalImage = true;
});
}
});
}
}