How to differentiate between horizontal scale, horizontal drag with one finger, horizontal drag with two fingers in Flutter?

61 Views Asked by At

I am doing a more complex custom chart requiring special treatment of three main actions:

  • user is scaling the widget horizontally (one finger going left, other finger going right)
  • user is dragging with one finger to the left or to the right
  • user is dragging with two fingers to the left or to the right.

Gestures

I tried using this code (and many other variations, but this seems to be the closest to the required behaviour):

class ChartGestureDetector extends StatelessWidget {
  final void Function(Offset localStartPosition, double delta) onDragWithOneFinger;
  final void Function(Offset localStartPosition, double delta) onDragWithTwoFingers;
  final void Function(Offset localStartPosition, double scale) onScale;
  final Widget child;

  ChartGestureDetector({
    required this.onDragWithOneFinger,
    required this.onDragWithTwoFingers,
    required this.onScale,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onScaleStart: (details) {
        // Handle scale start if needed
      },
      onScaleUpdate: (details) {
        if (details.scale != 1.0) {
          // Two-finger gesture (scale)
          onScale(details.localFocalPoint, details.scale);
        } else if (details.pointerCount == 1) {
          // One-finger gesture (pan)
          onDragWithOneFinger(details.localFocalPoint, details.horizontalScale);
        } else if (details.pointerCount == 2) {
          // Two-finger gesture (pan)
          onDragWithTwoFingers(details.localFocalPoint, details.horizontalScale);
        }
      },
      onScaleEnd: (details) {
        // Handle scale end if needed
      },
      child: child,
    );
  }
}```

Full demo here: https://pastebin.com/GNkHQ3k2


But it counts two finger drag as a scale.
How do I fix this code or what other approach should I take?
Is there any package that does what I am trying to do easier?
0

There are 0 best solutions below