From b5eb30faeb6c8601a385cbecbd61ca97b7c7ce1e Mon Sep 17 00:00:00 2001 From: zhangmeng <494089941@qq.com> Date: Tue, 19 Oct 2021 10:28:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=BE=E7=89=87=E9=A2=84?= =?UTF-8?q?=E8=A7=88=20=E6=B7=BB=E5=8A=A0=E5=8F=8C=E5=87=BB=E5=A4=8D?= =?UTF-8?q?=E5=8E=9F=E6=89=8B=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/as_grid_image_view.dart | 6 ++-- lib/utils/photo_viewer.dart | 59 ++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/lib/utils/as_grid_image_view.dart b/lib/utils/as_grid_image_view.dart index 92edf86..24e7245 100644 --- a/lib/utils/as_grid_image_view.dart +++ b/lib/utils/as_grid_image_view.dart @@ -32,7 +32,8 @@ class AsGridImageView extends StatelessWidget { children: files == null ? nets! .map((e) => GestureDetector( - onTap: () => PhotoViewer.toNet( + onTap: () => PhotoViewer.fromNet( + context, tag: e, net: e, ), @@ -45,7 +46,8 @@ class AsGridImageView extends StatelessWidget { .toList() : files! .map((e) => GestureDetector( - onTap: () => PhotoViewer.toFile(tag: e.path, file: e), + onTap: () => + PhotoViewer.fromFile(context, tag: e.path, file: e), child: Hero(tag: e.path, child: Image.file(e)), )) .toList()); diff --git a/lib/utils/photo_viewer.dart b/lib/utils/photo_viewer.dart index e56b405..151853a 100644 --- a/lib/utils/photo_viewer.dart +++ b/lib/utils/photo_viewer.dart @@ -55,16 +55,59 @@ class PhotoViewer extends StatefulWidget { _PhotoViewerState createState() => _PhotoViewerState(); } -class _PhotoViewerState extends State { +class _PhotoViewerState extends State + with SingleTickerProviderStateMixin { + TransformationController _controller = TransformationController(); + Animation? _animationReset; + late AnimationController _controllerReset; + + void reset() { + _controllerReset.reset(); + _animationReset = Matrix4Tween( + begin: _controller.value, + end: Matrix4.identity(), + ).animate(_controllerReset); + _animationReset!.addListener(_onAnimateReset); + _controllerReset.forward(); + } + + void _onAnimateReset() { + _controller.value = _animationReset!.value; + if (!_controllerReset.isAnimating) { + _animationReset?.removeListener(_onAnimateReset); + _animationReset = null; + _controllerReset.reset(); + } + } + + @override + void initState() { + _controllerReset = + AnimationController(vsync: this, duration: Duration(milliseconds: 400)); + super.initState(); + } + + @override + void dispose() { + _controllerReset.dispose(); + _controller.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { - return Scaffold( - backgroundColor: Colors.black54, - body: GestureDetector( - onTap: () => Navigator.pop(context), - child: Center( - child: InteractiveViewer( - boundaryMargin: 100.edge, + return GestureDetector( + onTap: () => Navigator.pop(context), + onDoubleTap: () => reset(), + child: Scaffold( + backgroundColor: Colors.black54, + body: InteractiveViewer( + transformationController: _controller, + boundaryMargin: 100.edge, + child: Container( + width: double.infinity, + height: double.infinity, + alignment: Alignment.center, child: Hero( tag: widget.tag, child: widget.file != null