2022-06-20 20:29:42 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:photo_view/photo_view.dart';
|
|
|
|
|
|
|
|
enum _RemoteImageStatus { empty, thumbnail, full }
|
|
|
|
|
|
|
|
class _RemotePhotoViewState extends State<RemotePhotoView> {
|
|
|
|
late CachedNetworkImageProvider _imageProvider;
|
|
|
|
_RemoteImageStatus _status = _RemoteImageStatus.empty;
|
|
|
|
bool _zoomedIn = false;
|
|
|
|
|
|
|
|
static const int swipeThreshold = 100;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
bool allowMoving = _status == _RemoteImageStatus.full;
|
|
|
|
return PhotoView(
|
2022-07-13 14:23:48 +02:00
|
|
|
imageProvider: _imageProvider,
|
|
|
|
minScale: PhotoViewComputedScale.contained,
|
|
|
|
maxScale: allowMoving ? 1.0 : PhotoViewComputedScale.contained,
|
|
|
|
enablePanAlways: true,
|
|
|
|
scaleStateChangedCallback: _scaleStateChanged,
|
|
|
|
onScaleEnd: _onScaleListener,
|
|
|
|
);
|
2022-06-20 20:29:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
void _onScaleListener(
|
|
|
|
BuildContext context,
|
|
|
|
ScaleEndDetails details,
|
|
|
|
PhotoViewControllerValue controllerValue,
|
|
|
|
) {
|
2022-06-20 20:29:42 +02:00
|
|
|
// Disable swipe events when zoomed in
|
2022-08-03 22:36:12 +02:00
|
|
|
if (_zoomedIn) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-20 20:29:42 +02:00
|
|
|
if (controllerValue.position.dy > swipeThreshold) {
|
|
|
|
widget.onSwipeDown();
|
|
|
|
} else if (controllerValue.position.dy < -swipeThreshold) {
|
|
|
|
widget.onSwipeUp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _scaleStateChanged(PhotoViewScaleState state) {
|
2022-08-03 22:36:12 +02:00
|
|
|
// _onScaleListener;
|
|
|
|
_zoomedIn = state != PhotoViewScaleState.initial;
|
|
|
|
if (_zoomedIn) {
|
|
|
|
widget.isZoomedListener.value = true;
|
|
|
|
} else {
|
|
|
|
widget.isZoomedListener.value = false;
|
|
|
|
}
|
|
|
|
widget.isZoomedFunction();
|
2022-06-20 20:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CachedNetworkImageProvider _authorizedImageProvider(String url) {
|
2022-07-13 14:23:48 +02:00
|
|
|
return CachedNetworkImageProvider(
|
|
|
|
url,
|
|
|
|
headers: {"Authorization": widget.authToken},
|
|
|
|
cacheKey: url,
|
|
|
|
);
|
2022-06-20 20:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _performStateTransition(
|
2022-07-13 14:23:48 +02:00
|
|
|
_RemoteImageStatus newStatus,
|
|
|
|
CachedNetworkImageProvider provider,
|
|
|
|
) {
|
2022-06-20 20:29:42 +02:00
|
|
|
// Transition to same status is forbidden
|
|
|
|
if (_status == newStatus) return;
|
|
|
|
// Transition full -> thumbnail is forbidden
|
|
|
|
if (_status == _RemoteImageStatus.full &&
|
|
|
|
newStatus == _RemoteImageStatus.thumbnail) return;
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_status = newStatus;
|
|
|
|
_imageProvider = provider;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _loadImages() {
|
|
|
|
CachedNetworkImageProvider thumbnailProvider =
|
|
|
|
_authorizedImageProvider(widget.thumbnailUrl);
|
|
|
|
_imageProvider = thumbnailProvider;
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
thumbnailProvider.resolve(const ImageConfiguration()).addListener(
|
|
|
|
ImageStreamListener((ImageInfo imageInfo, _) {
|
|
|
|
_performStateTransition(
|
|
|
|
_RemoteImageStatus.thumbnail,
|
|
|
|
thumbnailProvider,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
2022-06-20 20:29:42 +02:00
|
|
|
|
|
|
|
CachedNetworkImageProvider fullProvider =
|
|
|
|
_authorizedImageProvider(widget.imageUrl);
|
2022-07-13 14:23:48 +02:00
|
|
|
fullProvider.resolve(const ImageConfiguration()).addListener(
|
|
|
|
ImageStreamListener((ImageInfo imageInfo, _) {
|
|
|
|
_performStateTransition(_RemoteImageStatus.full, fullProvider);
|
|
|
|
}),
|
|
|
|
);
|
2022-06-20 20:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_loadImages();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemotePhotoView extends StatefulWidget {
|
2022-07-13 14:23:48 +02:00
|
|
|
const RemotePhotoView({
|
|
|
|
Key? key,
|
|
|
|
required this.thumbnailUrl,
|
|
|
|
required this.imageUrl,
|
|
|
|
required this.authToken,
|
2022-08-03 22:36:12 +02:00
|
|
|
required this.isZoomedFunction,
|
|
|
|
required this.isZoomedListener,
|
2022-07-13 14:23:48 +02:00
|
|
|
required this.onSwipeDown,
|
|
|
|
required this.onSwipeUp,
|
|
|
|
}) : super(key: key);
|
2022-06-20 20:29:42 +02:00
|
|
|
|
|
|
|
final String thumbnailUrl;
|
|
|
|
final String imageUrl;
|
|
|
|
final String authToken;
|
|
|
|
|
|
|
|
final void Function() onSwipeDown;
|
|
|
|
final void Function() onSwipeUp;
|
2022-08-03 22:36:12 +02:00
|
|
|
final void Function() isZoomedFunction;
|
|
|
|
|
|
|
|
final ValueNotifier<bool> isZoomedListener;
|
2022-06-20 20:29:42 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _RemotePhotoViewState();
|
|
|
|
}
|
|
|
|
}
|