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?
Use GestureDetector. Example
GestureDetector(
onHorizontalDragEnd: (dragDetail) {
if (dragDetail.velocity.pixelsPerSecond.dx < 1) {
print("right");
} else {
print("left");
}
},
child: Container(...),
);
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With