修复图片预览 添加双击复原手势

master
张萌 3 years ago
parent 14be6925b9
commit b5eb30faeb

@ -32,7 +32,8 @@ class AsGridImageView extends StatelessWidget {
children: files == null children: files == null
? nets! ? nets!
.map((e) => GestureDetector( .map((e) => GestureDetector(
onTap: () => PhotoViewer.toNet( onTap: () => PhotoViewer.fromNet(
context,
tag: e, tag: e,
net: e, net: e,
), ),
@ -45,7 +46,8 @@ class AsGridImageView extends StatelessWidget {
.toList() .toList()
: files! : files!
.map((e) => GestureDetector( .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)), child: Hero(tag: e.path, child: Image.file(e)),
)) ))
.toList()); .toList());

@ -55,16 +55,59 @@ class PhotoViewer extends StatefulWidget {
_PhotoViewerState createState() => _PhotoViewerState(); _PhotoViewerState createState() => _PhotoViewerState();
} }
class _PhotoViewerState extends State<PhotoViewer> { class _PhotoViewerState extends State<PhotoViewer>
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return GestureDetector(
backgroundColor: Colors.black54, onTap: () => Navigator.pop(context),
body: GestureDetector( onDoubleTap: () => reset(),
onTap: () => Navigator.pop(context), child: Scaffold(
child: Center( backgroundColor: Colors.black54,
child: InteractiveViewer( body: InteractiveViewer(
boundaryMargin: 100.edge, transformationController: _controller,
boundaryMargin: 100.edge,
child: Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.center,
child: Hero( child: Hero(
tag: widget.tag, tag: widget.tag,
child: widget.file != null child: widget.file != null

Loading…
Cancel
Save