From 92605e3873cc581ac7d330b9fcc60c2006014025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Vel=C3=A1squez=20L=C3=B3pez?= Date: Thu, 3 Jun 2021 14:17:37 -0500 Subject: [PATCH] [Feature] Added `enablePanAlways` to allow the user pan any view without restrictions. (#427) * allow pan even when the widget is smaller than the container * Enable always pan widget. --- .gitignore | 2 ++ example/pubspec.lock | 6 +++--- lib/photo_view.dart | 8 ++++++++ .../controller/photo_view_controller_delegate.dart | 4 +++- lib/src/core/photo_view_core.dart | 12 ++++++++---- lib/src/core/photo_view_gesture_detector.dart | 2 +- lib/src/photo_view_wrappers.dart | 6 ++++++ pubspec.lock | 2 +- 8 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 6ffedaea..61bfa687 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,5 @@ build/ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages +.fvm/* +.gitignore diff --git a/example/pubspec.lock b/example/pubspec.lock index 2a9f3157..8ea89fa5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.6.0" + version: "2.5.0" boolean_selector: dependency: transitive description: @@ -127,7 +127,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.0" stack_trace: dependency: transitive description: @@ -162,7 +162,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.3.0" + version: "0.2.19" typed_data: dependency: transitive description: diff --git a/lib/photo_view.dart b/lib/photo_view.dart index d3dd3de9..fc35fd14 100644 --- a/lib/photo_view.dart +++ b/lib/photo_view.dart @@ -257,6 +257,7 @@ class PhotoView extends StatefulWidget { this.filterQuality, this.disableGestures, this.errorBuilder, + this.enablePanAlways, }) : child = null, childSize = null, super(key: key); @@ -290,6 +291,7 @@ class PhotoView extends StatefulWidget { this.tightMode, this.filterQuality, this.disableGestures, + this.enablePanAlways, }) : errorBuilder = null, imageProvider = null, gaplessPlayback = false, @@ -388,6 +390,10 @@ class PhotoView extends StatefulWidget { // Useful when custom gesture detector is used in child widget. final bool? disableGestures; + /// Enable pan the widget even if it's smaller than the hole parent widget. + /// Useful when you want to drag a widget without restrictions. + final bool? enablePanAlways; + bool get _isCustomChild { return child != null; } @@ -505,6 +511,7 @@ class _PhotoViewState extends State { tightMode: widget.tightMode, filterQuality: widget.filterQuality, disableGestures: widget.disableGestures, + enablePanAlways: widget.enablePanAlways, ) : ImageWrapper( imageProvider: widget.imageProvider!, @@ -530,6 +537,7 @@ class _PhotoViewState extends State { filterQuality: widget.filterQuality, disableGestures: widget.disableGestures, errorBuilder: widget.errorBuilder, + enablePanAlways: widget.enablePanAlways, ); }, ); diff --git a/lib/src/controller/photo_view_controller_delegate.dart b/lib/src/controller/photo_view_controller_delegate.dart index 9cd1495e..8b88a80f 100644 --- a/lib/src/controller/photo_view_controller_delegate.dart +++ b/lib/src/controller/photo_view_controller_delegate.dart @@ -67,7 +67,9 @@ mixin PhotoViewControllerDelegate on State { } void _blindScaleListener() { - controller.position = clampPosition(); + if (!widget.enablePanAlways) { + controller.position = clampPosition(); + } if (controller.scale == controller.prevValue.scale) { return; } diff --git a/lib/src/core/photo_view_core.dart b/lib/src/core/photo_view_core.dart index 4d526b8d..e7a45177 100644 --- a/lib/src/core/photo_view_core.dart +++ b/lib/src/core/photo_view_core.dart @@ -41,7 +41,8 @@ class PhotoViewCore extends StatefulWidget { required this.tightMode, required this.filterQuality, required this.disableGestures, - }) : customChild = null, + required this.enablePanAlways, + }) : customChild = null, super(key: key); const PhotoViewCore.customChild({ @@ -62,7 +63,8 @@ class PhotoViewCore extends StatefulWidget { required this.tightMode, required this.filterQuality, required this.disableGestures, - }) : imageProvider = null, + required this.enablePanAlways, + }) : imageProvider = null, gaplessPlayback = false, super(key: key); @@ -86,6 +88,7 @@ class PhotoViewCore extends StatefulWidget { final HitTestBehavior? gestureDetectorBehavior; final bool tightMode; final bool disableGestures; + final bool enablePanAlways; final FilterQuality filterQuality; @@ -151,10 +154,11 @@ class PhotoViewCoreState extends State updateScaleStateFromNewScale(newScale); - // updateMultiple( scale: newScale, - position: clampPosition(position: delta * details.scale), + position: widget.enablePanAlways + ? delta + : clampPosition(position: delta * details.scale), rotation: widget.enableRotation ? _rotationBefore! + details.rotation : null, rotationFocusPoint: widget.enableRotation ? details.focalPoint : null, diff --git a/lib/src/core/photo_view_gesture_detector.dart b/lib/src/core/photo_view_gesture_detector.dart index 1edf1413..7742783d 100644 --- a/lib/src/core/photo_view_gesture_detector.dart +++ b/lib/src/core/photo_view_gesture_detector.dart @@ -98,7 +98,7 @@ class PhotoViewGestureRecognizer extends ScaleGestureRecognizer { bool ready = true; @override - void addAllowedPointer(PointerDownEvent event) { + void addAllowedPointer(PointerEvent event) { if (ready) { ready = false; _pointerLocations = {}; diff --git a/lib/src/photo_view_wrappers.dart b/lib/src/photo_view_wrappers.dart index 979cff80..9cd8ac0a 100644 --- a/lib/src/photo_view_wrappers.dart +++ b/lib/src/photo_view_wrappers.dart @@ -31,6 +31,7 @@ class ImageWrapper extends StatefulWidget { required this.filterQuality, required this.disableGestures, required this.errorBuilder, + required this.enablePanAlways, }) : super(key: key); final ImageProvider imageProvider; @@ -56,6 +57,7 @@ class ImageWrapper extends StatefulWidget { final bool? tightMode; final FilterQuality? filterQuality; final bool? disableGestures; + final bool? enablePanAlways; @override _ImageWrapperState createState() => _ImageWrapperState(); @@ -187,6 +189,7 @@ class _ImageWrapperState extends State { tightMode: widget.tightMode ?? false, filterQuality: widget.filterQuality ?? FilterQuality.none, disableGestures: widget.disableGestures ?? false, + enablePanAlways: widget.enablePanAlways ?? false, ); } @@ -236,6 +239,7 @@ class CustomChildWrapper extends StatelessWidget { required this.tightMode, required this.filterQuality, required this.disableGestures, + required this.enablePanAlways, }) : super(key: key); final Widget? child; @@ -262,6 +266,7 @@ class CustomChildWrapper extends StatelessWidget { final bool? tightMode; final FilterQuality? filterQuality; final bool? disableGestures; + final bool? enablePanAlways; @override Widget build(BuildContext context) { @@ -290,6 +295,7 @@ class CustomChildWrapper extends StatelessWidget { tightMode: tightMode ?? false, filterQuality: filterQuality ?? FilterQuality.none, disableGestures: disableGestures ?? false, + enablePanAlways: enablePanAlways ?? false, ); } } diff --git a/pubspec.lock b/pubspec.lock index 488968cf..efe38139 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -28,7 +28,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.6.0" + version: "2.6.1" boolean_selector: dependency: transitive description: