Skip to content

Commit

Permalink
* Fix issue that unsmooth zooming when using ExtendedImageGesturePage…
Browse files Browse the repository at this point in the history
…View (#631)

* Add [ExtendedImageGesturePageView.shouldAccpetHorizontalOrVerticalDrag] to custum whether should accpet horizontal or vertical drag at that time.
  • Loading branch information
zmtzawqlp committed Oct 2, 2024
1 parent be9e83a commit 043b73a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 8.0.3

* Fix issue that unsmooth zooming when using ExtendedImageGesturePageView (#631)
* Add [ExtendedImageGesturePageView.shouldAccpetHorizontalOrVerticalDrag] to custum whether should accpet horizontal or vertical drag at that time.

## 8.0.2

* [EditorCropLayerPainter.paintMask] not use BlendMode.clear now, due to '--web-renderer html' is not support.
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
assets.preview.dart
22 changes: 22 additions & 0 deletions example/lib/pages/simple/photo_view_demo.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:extended_image/extended_image.dart';
import 'package:ff_annotation_route_core/ff_annotation_route_core.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

@FFRoute(
Expand Down Expand Up @@ -57,6 +58,27 @@ class _SimplePhotoViewDemoState extends State<SimplePhotoViewDemo> {
},
);
},
// just demo, it the same as default.
// if you need to custom it, you can define it base on your case.
shouldAccpetHorizontalOrVerticalDrag:
(Map<int, VelocityTracker> velocityTrackers) {
if (velocityTrackers.keys.length == 1) {
return true;
}

// if pointers are not the only, check whether they are in the negative
// maybe this is a Horizontal/Vertical zoom
Offset offset = const Offset(1, 1);
for (final VelocityTracker tracker in velocityTrackers.values) {
if (tracker is ExtendedVelocityTracker) {
final Offset delta = tracker.getSamplesDelta();
offset = Offset(offset.dx * (delta.dx == 0 ? 1 : delta.dx),
offset.dy * (delta.dy == 0 ? 1 : delta.dy));
}
}

return !(offset.dx < 0 || offset.dy < 0);
},
),
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/extended_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export 'src/gesture/page_view/gesture_page_view.dart';
export 'src/gesture/slide_page.dart';
export 'src/gesture/slide_page_handler.dart';
export 'src/gesture/utils.dart';
export 'src/gesture_detector/official.dart';
export 'src/image/painting.dart';
export 'src/image/raw_image.dart';
export 'src/image/render_image.dart';
Expand Down
12 changes: 12 additions & 0 deletions lib/src/gesture/page_view/gesture_page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ExtendedImageGesturePageView extends StatefulWidget {
List<Widget> children = const <Widget>[],
CanScrollPage? canScrollPage,
this.preloadPagesCount = 0,
this.shouldAccpetHorizontalOrVerticalDrag,
}) : controller = controller ?? _defaultPageController,
childrenDelegate = SliverChildListDelegate(children),
physics = physics != null
Expand Down Expand Up @@ -80,6 +81,7 @@ class ExtendedImageGesturePageView extends StatefulWidget {
int? itemCount,
CanScrollPage? canScrollPage,
this.preloadPagesCount = 0,
this.shouldAccpetHorizontalOrVerticalDrag,
}) : controller = controller ?? _defaultPageController,
childrenDelegate =
SliverChildBuilderDelegate(itemBuilder, childCount: itemCount),
Expand All @@ -102,6 +104,7 @@ class ExtendedImageGesturePageView extends StatefulWidget {
CanScrollPage? canScrollPage,
required this.childrenDelegate,
this.preloadPagesCount = 0,
this.shouldAccpetHorizontalOrVerticalDrag,
}) : controller = controller ?? _defaultPageController,
physics = _defaultScrollPhysics,
canScrollPage = canScrollPage ?? _defaultCanScrollPage,
Expand Down Expand Up @@ -161,6 +164,11 @@ class ExtendedImageGesturePageView extends StatefulWidget {
/// The count of pre-built pages
final int preloadPagesCount;

/// Whether should accpet horizontal or vertical drag at that time
/// You can custom it by your base
final ShouldAccpetHorizontalOrVerticalDrag?
shouldAccpetHorizontalOrVerticalDrag;

@override
ExtendedImageGesturePageViewState createState() =>
ExtendedImageGesturePageViewState();
Expand Down Expand Up @@ -230,6 +238,8 @@ class ExtendedImageGesturePageViewState
() => ExtendedVerticalDragGestureRecognizer(
canHorizontalOrVerticalDrag: canHorizontalOrVerticalDrag,
debugOwner: this,
shouldAccpetHorizontalOrVerticalDrag:
widget.shouldAccpetHorizontalOrVerticalDrag,
),
(ExtendedVerticalDragGestureRecognizer instance) {
instance
Expand All @@ -254,6 +264,8 @@ class ExtendedImageGesturePageViewState
() => ExtendedHorizontalDragGestureRecognizer(
canHorizontalOrVerticalDrag: canHorizontalOrVerticalDrag,
debugOwner: this,
shouldAccpetHorizontalOrVerticalDrag:
widget.shouldAccpetHorizontalOrVerticalDrag,
),
(ExtendedHorizontalDragGestureRecognizer instance) {
instance
Expand Down
38 changes: 23 additions & 15 deletions lib/src/gesture_detector/drag_gesture_recognizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ part of 'official.dart';

typedef CanHorizontalOrVerticalDrag = bool Function();

typedef ShouldAccpetHorizontalOrVerticalDrag = bool Function(
Map<int, VelocityTracker> velocityTrackers,
);

mixin DragGestureRecognizerMixin on _DragGestureRecognizer {
bool get canDrag =>
canHorizontalOrVerticalDrag == null || canHorizontalOrVerticalDrag!();
Expand All @@ -10,6 +14,10 @@ mixin DragGestureRecognizerMixin on _DragGestureRecognizer {
if (!canDrag) {
return false;
}
if (shouldAccpetHorizontalOrVerticalDrag != null) {
return shouldAccpetHorizontalOrVerticalDrag!(_velocityTrackers);
}

if (_velocityTrackers.keys.length == 1) {
return true;
}
Expand All @@ -29,6 +37,8 @@ mixin DragGestureRecognizerMixin on _DragGestureRecognizer {
}

CanHorizontalOrVerticalDrag? get canHorizontalOrVerticalDrag;
ShouldAccpetHorizontalOrVerticalDrag?
get shouldAccpetHorizontalOrVerticalDrag;

@override
void handleEvent(PointerEvent event) {
Expand Down Expand Up @@ -101,36 +111,30 @@ mixin DragGestureRecognizerMixin on _DragGestureRecognizer {
_giveUpPointer(event.pointer);
}
}
}

abstract class ExtendedDragGestureRecognizer extends _DragGestureRecognizer
with DragGestureRecognizerMixin {
ExtendedDragGestureRecognizer({
super.debugOwner,
super.dragStartBehavior = DragStartBehavior.start,
super.velocityTrackerBuilder = _defaultBuilder,
super.supportedDevices,
super.allowedButtonsFilter,
this.canHorizontalOrVerticalDrag,
});

static ExtendedVelocityTracker _defaultBuilder(PointerEvent event) =>
ExtendedVelocityTracker.withKind(event.kind);
@override
final CanHorizontalOrVerticalDrag? canHorizontalOrVerticalDrag;
GestureVelocityTrackerBuilder get velocityTrackerBuilder => _defaultBuilder;
}

ExtendedVelocityTracker _defaultBuilder(PointerEvent event) =>
ExtendedVelocityTracker.withKind(event.kind);

class ExtendedHorizontalDragGestureRecognizer
extends _HorizontalDragGestureRecognizer with DragGestureRecognizerMixin {
ExtendedHorizontalDragGestureRecognizer({
super.debugOwner,
super.supportedDevices,
super.allowedButtonsFilter,
this.canHorizontalOrVerticalDrag,
this.shouldAccpetHorizontalOrVerticalDrag,
});

@override
final CanHorizontalOrVerticalDrag? canHorizontalOrVerticalDrag;

@override
final ShouldAccpetHorizontalOrVerticalDrag?
shouldAccpetHorizontalOrVerticalDrag;
}

class ExtendedVerticalDragGestureRecognizer
Expand All @@ -140,8 +144,12 @@ class ExtendedVerticalDragGestureRecognizer
super.supportedDevices,
super.allowedButtonsFilter,
this.canHorizontalOrVerticalDrag,
this.shouldAccpetHorizontalOrVerticalDrag,
});

@override
final CanHorizontalOrVerticalDrag? canHorizontalOrVerticalDrag;
@override
final ShouldAccpetHorizontalOrVerticalDrag?
shouldAccpetHorizontalOrVerticalDrag;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: extended_image
description: Official extension image, support placeholder(loading)/ failed state, cache network, zoom/pan, photo view, slide out page, editor(crop,rotate,flip), painting etc.
version: 8.0.2
version: 8.0.3
homepage: https://github.com/fluttercandies/extended_image

environment:
Expand Down

0 comments on commit 043b73a

Please sign in to comment.