Skip to content

Commit

Permalink
Add ability to disable gestures (Solves #233) (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo authored Aug 17, 2020
2 parents 89534ca + 6567f78 commit 9adbf49
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
3 changes: 3 additions & 0 deletions example/lib/screens/common/common_example_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
this.initialScale,
this.basePosition = Alignment.center,
this.filterQuality = FilterQuality.none,
this.disableGestures,
});

final ImageProvider imageProvider;
Expand All @@ -21,6 +22,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
final dynamic initialScale;
final Alignment basePosition;
final FilterQuality filterQuality;
final bool disableGestures;

@override
Widget build(BuildContext context) {
Expand All @@ -38,6 +40,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
initialScale: initialScale,
basePosition: basePosition,
filterQuality: filterQuality,
disableGestures: disableGestures,
),
),
);
Expand Down
14 changes: 14 additions & 0 deletions example/lib/screens/examples/common_use_cases_examples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ class CommonUseCasesExamples extends StatelessWidget {
);
},
),
ExampleButtonNode(
title: "No gesture ",
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CommonExampleRouteWrapper(
imageProvider: const AssetImage("assets/large-image.jpg"),
disableGestures: true,
),
),
);
},
),
],
),
);
Expand Down
8 changes: 8 additions & 0 deletions lib/photo_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class PhotoView extends StatefulWidget {
this.gestureDetectorBehavior,
this.tightMode,
this.filterQuality,
this.disableGestures,
}) : child = null,
childSize = null,
super(key: key);
Expand Down Expand Up @@ -289,6 +290,7 @@ class PhotoView extends StatefulWidget {
this.gestureDetectorBehavior,
this.tightMode,
this.filterQuality,
this.disableGestures,
}) : loadFailedChild = null,
imageProvider = null,
gaplessPlayback = false,
Expand Down Expand Up @@ -379,6 +381,10 @@ class PhotoView extends StatefulWidget {
/// Quality levels for image filters.
final FilterQuality filterQuality;

// Removes gesture detector if `true`.
// Useful when custom gesture detector is used in child widget.
final bool disableGestures;

@override
State<StatefulWidget> createState() {
return _PhotoViewState();
Expand Down Expand Up @@ -552,6 +558,7 @@ class _PhotoViewState extends State<PhotoView> {
gestureDetectorBehavior: widget.gestureDetectorBehavior,
tightMode: widget.tightMode ?? false,
filterQuality: widget.filterQuality ?? FilterQuality.none,
disableGestures: widget.disableGestures ?? false,
);
}

Expand Down Expand Up @@ -609,6 +616,7 @@ class _PhotoViewState extends State<PhotoView> {
gestureDetectorBehavior: widget.gestureDetectorBehavior,
tightMode: widget.tightMode ?? false,
filterQuality: widget.filterQuality ?? FilterQuality.none,
disableGestures: widget.disableGestures ?? false,
);
}

Expand Down
9 changes: 8 additions & 1 deletion lib/photo_view_gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
tightMode: pageOption.tightMode,
filterQuality: pageOption.filterQuality,
basePosition: pageOption.basePosition,
disableGestures: pageOption.disableGestures,
)
: PhotoView(
key: ObjectKey(index),
Expand All @@ -289,6 +290,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
tightMode: pageOption.tightMode,
filterQuality: pageOption.filterQuality,
basePosition: pageOption.basePosition,
disableGestures: pageOption.disableGestures,
);

return ClipRect(
Expand Down Expand Up @@ -326,11 +328,12 @@ class PhotoViewGalleryPageOptions {
this.gestureDetectorBehavior,
this.tightMode,
this.filterQuality,
this.disableGestures,
}) : child = null,
childSize = null,
assert(imageProvider != null);

PhotoViewGalleryPageOptions.customChild({
PhotoViewGalleryPageOptions.customChild( {
@required this.child,
this.childSize,
this.heroAttributes,
Expand All @@ -346,6 +349,7 @@ class PhotoViewGalleryPageOptions {
this.gestureDetectorBehavior,
this.tightMode,
this.filterQuality,
this.disableGestures,
}) : imageProvider = null,
assert(child != null);

Expand Down Expand Up @@ -394,6 +398,9 @@ class PhotoViewGalleryPageOptions {
/// Mirror to [PhotoView.tightMode]
final bool tightMode;

/// Mirror to [PhotoView.disableGestures]
final bool disableGestures;

/// Quality levels for image filters.
final FilterQuality filterQuality;
}
34 changes: 22 additions & 12 deletions lib/src/core/photo_view_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PhotoViewCore extends StatefulWidget {
@required this.basePosition,
@required this.tightMode,
@required this.filterQuality,
@required this.disableGestures,
}) : customChild = null,
super(key: key);

Expand All @@ -57,6 +58,7 @@ class PhotoViewCore extends StatefulWidget {
@required this.basePosition,
@required this.tightMode,
@required this.filterQuality,
@required this.disableGestures,
}) : imageProvider = null,
gaplessPlayback = false,
super(key: key);
Expand All @@ -79,6 +81,7 @@ class PhotoViewCore extends StatefulWidget {

final HitTestBehavior gestureDetectorBehavior;
final bool tightMode;
final bool disableGestures;

final FilterQuality filterQuality;

Expand Down Expand Up @@ -312,20 +315,27 @@ class PhotoViewCoreState extends State<PhotoViewCore>
),
child: _buildHero(),
);
return PhotoViewGestureDetector(
child: Container(
constraints: widget.tightMode
? BoxConstraints.tight(scaleBoundaries.childSize * scale)
: null,
child: Center(
child: Transform(
child: customChildLayout,
transform: matrix,
alignment: basePosition,
),

final child = Container(
constraints: widget.tightMode
? BoxConstraints.tight(scaleBoundaries.childSize * scale)
: null,
child: Center(
child: Transform(
child: customChildLayout,
transform: matrix,
alignment: basePosition,
),
decoration: widget.backgroundDecoration ?? _defaultDecoration,
),
decoration: widget.backgroundDecoration ?? _defaultDecoration,
);

if (widget.disableGestures) {
return child;
}

return PhotoViewGestureDetector(
child: child,
onDoubleTap: nextScaleState,
onScaleStart: onScaleStart,
onScaleUpdate: onScaleUpdate,
Expand Down

0 comments on commit 9adbf49

Please sign in to comment.