0

Here is my code, the onVerticalDragUpdate called multiple times on swipe down/up. I tried to adjust sensitivity from zero. that not worked for me.

GestureDetector(
       onVerticalDragUpdate: (details) {
          if (details.delta.dy > 0) {
              print('------Down Swipe----');
                 changePage(currentPage++);
             }
       }
)

Also, I tried onPanUpdate which has the same problem.

GestureDetector(onPanUpdate: (details) {
   if (details.delta.dy > 0) {
    // swiping
   }
});
AleksanderGD
  • 414
  • 5
  • 10
ajay
  • 808
  • 9
  • 18

2 Answers2

0

That's the point of this function. Based on the documentation:

A pointer that is in contact with the screen with a primary button and moving vertically has moved in the vertical direction.

Meaning, this function would be called every time the vertical position changes on drag.

If you want to execute the function only once, use onVerticalDragStart or onVerticalDragEnd (based on what you want to achieve).

mkobuolys
  • 4,499
  • 1
  • 11
  • 27
  • How can I achieve vertical swipe up/down? The function inside need not call multiple time. – ajay May 17 '21 at 16:33
  • 1
    Did you check for answers [here](https://stackoverflow.com/questions/55050463/how-to-detect-swipe-in-flutter)? – mkobuolys May 17 '21 at 19:03
  • Yes. But the problem is the function inside calling multiple times. – ajay May 18 '21 at 10:26
  • 1
    Maybe you can improve the function, like having a flag that would change false/true when function is executed once? – mkobuolys May 18 '21 at 10:57
0

I might be a little late on this one but I went through the same issue and found the solution.

You need to combine

  • onPanUpdate to get the direction and set a variable
  • onPanEnd to set the state only when the move is finished

Example :

return GestureDetector(
  onPanEnd: (details) {
    if (direction == 'right') {
      activeIndex = (activeIndex! + 1);
      setState(() {});
    }
    if (direction == 'left') {
      activeIndex = (activeIndex! - 1);
      setState(() {});
    } else {}
  },
  onPanUpdate: (details) {
    // Swiping in right direction.
    if (details.delta.dx > 0) {
      direction = 'right';
    }

    // Swiping in left direction.
    if (details.delta.dx < 0) {
      direction = 'left';
    }
  },
  child: Scaffold(...
TomBuv
  • 33
  • 3