Flutter - Why onVerticalDragEnd does not work?

1,700

Solution 1

Instead of GestureDetector, you can use Listener, it worked for me. Here's how I implemented it -

import 'dart:math';
double radians(double degree) {
    return ((degree * 180) / pi);
  }
void swipe(moveEvent) {
double angle = radians(moveEvent.delta.direction);
    if (angle >= -45 && angle <= 45) {
      print("Swipe Right");
    } else if (angle >= 45 && angle <= 135) {
      print("Swipe Down");
    } else if (angle <= -45 && angle >= -135) {
      print("Swipe Up");
    } else {
      print("Swipe Left");
    }
}
child: Listener(
  onPointerMove: (moveEvent) => swipe(moveEvent),
  child: GridView.count(...),
)

But this calls function multiple times on a single swipe

Solution 2

Try logging when your GestureDetector and/or parent components are being created, and see if it's being re-rendered before the End event. I had the same problem, and it was due to side effects of the Update event I used causing a re-render. After making some modifications to prevent that from happening, the End event started working.

Share:
1,700
Adi Albu
Author by

Adi Albu

Updated on December 05, 2022

Comments

  • Adi Albu
    Adi Albu over 1 year

    I am trying to get if the user is swiping horizontally or vertically using a GestureDetector widget. For some reason I am unable to make the onVerticalDragEnd property work. HorizontalDragEnd works just fine.

    Am I supposed to add anything else?

    `

    child: new GestureDetector(
                      onHorizontalDragEnd: (DragEndDetails details) {
                        print("horizontal drag");
                      },
                      onVerticalDragEnd : (DragEndDetails details) {
                        print("vertical drag");
                      } ,
                      child: new GridView.count(..
    

    `

    • Rémi Rousselet
      Rémi Rousselet almost 6 years
      You most likely have another widget somewhere that catch this gesture. Such as ListView.
  • Adi Albu
    Adi Albu almost 6 years
    If I delete onHorizontalDrag it still doesn't execute the vertical one. Furthermore, both horizontal and vertical drag work together just fine if I don't use GridView as a child. Is this a bug or just a constraint?
  • Nash
    Nash almost 6 years
    The reason it works is because you are using a grid view :P