Skip to content

Commit

Permalink
[Feature] Added enablePanAlways to allow the user pan any view with…
Browse files Browse the repository at this point in the history
…out restrictions. (#427)

* allow pan even when the widget is smaller than the container

* Enable always pan widget.
  • Loading branch information
diegoveloper authored Jun 3, 2021
1 parent 52685ab commit 92605e3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
.fvm/*
.gitignore
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions lib/photo_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class PhotoView extends StatefulWidget {
this.filterQuality,
this.disableGestures,
this.errorBuilder,
this.enablePanAlways,
}) : child = null,
childSize = null,
super(key: key);
Expand Down Expand Up @@ -290,6 +291,7 @@ class PhotoView extends StatefulWidget {
this.tightMode,
this.filterQuality,
this.disableGestures,
this.enablePanAlways,
}) : errorBuilder = null,
imageProvider = null,
gaplessPlayback = false,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -505,6 +511,7 @@ class _PhotoViewState extends State<PhotoView> {
tightMode: widget.tightMode,
filterQuality: widget.filterQuality,
disableGestures: widget.disableGestures,
enablePanAlways: widget.enablePanAlways,
)
: ImageWrapper(
imageProvider: widget.imageProvider!,
Expand All @@ -530,6 +537,7 @@ class _PhotoViewState extends State<PhotoView> {
filterQuality: widget.filterQuality,
disableGestures: widget.disableGestures,
errorBuilder: widget.errorBuilder,
enablePanAlways: widget.enablePanAlways,
);
},
);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/controller/photo_view_controller_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ mixin PhotoViewControllerDelegate on State<PhotoViewCore> {
}

void _blindScaleListener() {
controller.position = clampPosition();
if (!widget.enablePanAlways) {
controller.position = clampPosition();
}
if (controller.scale == controller.prevValue.scale) {
return;
}
Expand Down
12 changes: 8 additions & 4 deletions lib/src/core/photo_view_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);

Expand All @@ -86,6 +88,7 @@ class PhotoViewCore extends StatefulWidget {
final HitTestBehavior? gestureDetectorBehavior;
final bool tightMode;
final bool disableGestures;
final bool enablePanAlways;

final FilterQuality filterQuality;

Expand Down Expand Up @@ -151,10 +154,11 @@ class PhotoViewCoreState extends State<PhotoViewCore>

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,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/photo_view_gesture_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <int, Offset>{};
Expand Down
6 changes: 6 additions & 0 deletions lib/src/photo_view_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,6 +57,7 @@ class ImageWrapper extends StatefulWidget {
final bool? tightMode;
final FilterQuality? filterQuality;
final bool? disableGestures;
final bool? enablePanAlways;

@override
_ImageWrapperState createState() => _ImageWrapperState();
Expand Down Expand Up @@ -187,6 +189,7 @@ class _ImageWrapperState extends State<ImageWrapper> {
tightMode: widget.tightMode ?? false,
filterQuality: widget.filterQuality ?? FilterQuality.none,
disableGestures: widget.disableGestures ?? false,
enablePanAlways: widget.enablePanAlways ?? false,
);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -290,6 +295,7 @@ class CustomChildWrapper extends StatelessWidget {
tightMode: tightMode ?? false,
filterQuality: filterQuality ?? FilterQuality.none,
disableGestures: disableGestures ?? false,
enablePanAlways: enablePanAlways ?? false,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 92605e3

Please sign in to comment.