Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect left and right swipes in flutter?

Tags:

flutter

I am using GestureDetector to check when a user swipes horizontally across the screen.

The swipe should only be registered when the user removes their finger from the screen to end the swipe, hence the use of onHorizontalDragEnd.

When the swipe ends a int is incremented resulting in a new image being displayed.

The issue I am having is that I want to be able to let the user swipe left to go back an image.

How can I detect which direction a user has swiped when implementing onHorizontalDragEnd?

like image 973
CJW Avatar asked Oct 27 '25 08:10

CJW


2 Answers

Use GestureDetector. Example

GestureDetector(
      onHorizontalDragEnd: (dragDetail) {
        if (dragDetail.velocity.pixelsPerSecond.dx < 1) {
          print("right");
        } else {
          print("left");
        }
      },
      child: Container(...),
    );

like image 124
Shahriar Nasim Nafi Avatar answered Oct 28 '25 23:10

Shahriar Nasim Nafi


You can detect swipes using the onPanUpdate method from GestureDetector class.

GestureDetector(onPanUpdate: (details) {
  if (details.delta.dx > 0)
    print("Dragging in +X direction");
  else
    print("Dragging in -X direction");

  if (details.delta.dy > 0)
    print("Dragging in +Y direction");
  else
    print("Dragging in -Y direction");
});

Note: Do not use this method with the ones that you are already using (onVerticalDragDown) or onVerticalDragUpdate().

like image 45
Yudhishthir Singh Avatar answered Oct 28 '25 22:10

Yudhishthir Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!