Skip to content

Commit 9adbf49

Browse files
authored
Add ability to disable gestures (Solves #233) (#234)
2 parents 89534ca + 6567f78 commit 9adbf49

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

example/lib/screens/common/common_example_wrapper.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
1111
this.initialScale,
1212
this.basePosition = Alignment.center,
1313
this.filterQuality = FilterQuality.none,
14+
this.disableGestures,
1415
});
1516

1617
final ImageProvider imageProvider;
@@ -21,6 +22,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
2122
final dynamic initialScale;
2223
final Alignment basePosition;
2324
final FilterQuality filterQuality;
25+
final bool disableGestures;
2426

2527
@override
2628
Widget build(BuildContext context) {
@@ -38,6 +40,7 @@ class CommonExampleRouteWrapper extends StatelessWidget {
3840
initialScale: initialScale,
3941
basePosition: basePosition,
4042
filterQuality: filterQuality,
43+
disableGestures: disableGestures,
4144
),
4245
),
4346
);

example/lib/screens/examples/common_use_cases_examples.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ class CommonUseCasesExamples extends StatelessWidget {
131131
);
132132
},
133133
),
134+
ExampleButtonNode(
135+
title: "No gesture ",
136+
onPressed: () {
137+
Navigator.push(
138+
context,
139+
MaterialPageRoute(
140+
builder: (context) => const CommonExampleRouteWrapper(
141+
imageProvider: const AssetImage("assets/large-image.jpg"),
142+
disableGestures: true,
143+
),
144+
),
145+
);
146+
},
147+
),
134148
],
135149
),
136150
);

lib/photo_view.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class PhotoView extends StatefulWidget {
258258
this.gestureDetectorBehavior,
259259
this.tightMode,
260260
this.filterQuality,
261+
this.disableGestures,
261262
}) : child = null,
262263
childSize = null,
263264
super(key: key);
@@ -289,6 +290,7 @@ class PhotoView extends StatefulWidget {
289290
this.gestureDetectorBehavior,
290291
this.tightMode,
291292
this.filterQuality,
293+
this.disableGestures,
292294
}) : loadFailedChild = null,
293295
imageProvider = null,
294296
gaplessPlayback = false,
@@ -379,6 +381,10 @@ class PhotoView extends StatefulWidget {
379381
/// Quality levels for image filters.
380382
final FilterQuality filterQuality;
381383

384+
// Removes gesture detector if `true`.
385+
// Useful when custom gesture detector is used in child widget.
386+
final bool disableGestures;
387+
382388
@override
383389
State<StatefulWidget> createState() {
384390
return _PhotoViewState();
@@ -552,6 +558,7 @@ class _PhotoViewState extends State<PhotoView> {
552558
gestureDetectorBehavior: widget.gestureDetectorBehavior,
553559
tightMode: widget.tightMode ?? false,
554560
filterQuality: widget.filterQuality ?? FilterQuality.none,
561+
disableGestures: widget.disableGestures ?? false,
555562
);
556563
}
557564

@@ -609,6 +616,7 @@ class _PhotoViewState extends State<PhotoView> {
609616
gestureDetectorBehavior: widget.gestureDetectorBehavior,
610617
tightMode: widget.tightMode ?? false,
611618
filterQuality: widget.filterQuality ?? FilterQuality.none,
619+
disableGestures: widget.disableGestures ?? false,
612620
);
613621
}
614622

lib/photo_view_gallery.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
265265
tightMode: pageOption.tightMode,
266266
filterQuality: pageOption.filterQuality,
267267
basePosition: pageOption.basePosition,
268+
disableGestures: pageOption.disableGestures,
268269
)
269270
: PhotoView(
270271
key: ObjectKey(index),
@@ -289,6 +290,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
289290
tightMode: pageOption.tightMode,
290291
filterQuality: pageOption.filterQuality,
291292
basePosition: pageOption.basePosition,
293+
disableGestures: pageOption.disableGestures,
292294
);
293295

294296
return ClipRect(
@@ -326,11 +328,12 @@ class PhotoViewGalleryPageOptions {
326328
this.gestureDetectorBehavior,
327329
this.tightMode,
328330
this.filterQuality,
331+
this.disableGestures,
329332
}) : child = null,
330333
childSize = null,
331334
assert(imageProvider != null);
332335

333-
PhotoViewGalleryPageOptions.customChild({
336+
PhotoViewGalleryPageOptions.customChild( {
334337
@required this.child,
335338
this.childSize,
336339
this.heroAttributes,
@@ -346,6 +349,7 @@ class PhotoViewGalleryPageOptions {
346349
this.gestureDetectorBehavior,
347350
this.tightMode,
348351
this.filterQuality,
352+
this.disableGestures,
349353
}) : imageProvider = null,
350354
assert(child != null);
351355

@@ -394,6 +398,9 @@ class PhotoViewGalleryPageOptions {
394398
/// Mirror to [PhotoView.tightMode]
395399
final bool tightMode;
396400

401+
/// Mirror to [PhotoView.disableGestures]
402+
final bool disableGestures;
403+
397404
/// Quality levels for image filters.
398405
final FilterQuality filterQuality;
399406
}

lib/src/core/photo_view_core.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PhotoViewCore extends StatefulWidget {
3838
@required this.basePosition,
3939
@required this.tightMode,
4040
@required this.filterQuality,
41+
@required this.disableGestures,
4142
}) : customChild = null,
4243
super(key: key);
4344

@@ -57,6 +58,7 @@ class PhotoViewCore extends StatefulWidget {
5758
@required this.basePosition,
5859
@required this.tightMode,
5960
@required this.filterQuality,
61+
@required this.disableGestures,
6062
}) : imageProvider = null,
6163
gaplessPlayback = false,
6264
super(key: key);
@@ -79,6 +81,7 @@ class PhotoViewCore extends StatefulWidget {
7981

8082
final HitTestBehavior gestureDetectorBehavior;
8183
final bool tightMode;
84+
final bool disableGestures;
8285

8386
final FilterQuality filterQuality;
8487

@@ -312,20 +315,27 @@ class PhotoViewCoreState extends State<PhotoViewCore>
312315
),
313316
child: _buildHero(),
314317
);
315-
return PhotoViewGestureDetector(
316-
child: Container(
317-
constraints: widget.tightMode
318-
? BoxConstraints.tight(scaleBoundaries.childSize * scale)
319-
: null,
320-
child: Center(
321-
child: Transform(
322-
child: customChildLayout,
323-
transform: matrix,
324-
alignment: basePosition,
325-
),
318+
319+
final child = Container(
320+
constraints: widget.tightMode
321+
? BoxConstraints.tight(scaleBoundaries.childSize * scale)
322+
: null,
323+
child: Center(
324+
child: Transform(
325+
child: customChildLayout,
326+
transform: matrix,
327+
alignment: basePosition,
326328
),
327-
decoration: widget.backgroundDecoration ?? _defaultDecoration,
328329
),
330+
decoration: widget.backgroundDecoration ?? _defaultDecoration,
331+
);
332+
333+
if (widget.disableGestures) {
334+
return child;
335+
}
336+
337+
return PhotoViewGestureDetector(
338+
child: child,
329339
onDoubleTap: nextScaleState,
330340
onScaleStart: onScaleStart,
331341
onScaleUpdate: onScaleUpdate,

0 commit comments

Comments
 (0)