Flutter onVerticalDragEnd不起作用

时间:2018-07-09 20:37:43

标签: android dart flutter

我正在尝试获取用户是使用GestureDetector小部件水平还是垂直滑动。由于某些原因,我无法使onVerticalDragEnd属性起作用。 Horizo​​ntalDragEnd可以正常工作。

我还应该添加其他内容吗?

`

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

`

4 个答案:

答案 0 :(得分:1)

您可以使用 Listener 代替 GestureDetector,它对我有用。这是我实现它的方式 -

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(...),
)

但是这会在一次滑动中多次调用函数

答案 1 :(得分:0)

美好的一天

之所以不起作用,是因为您不能同时指定onHorizontalDragonVerticalDrag。这样做将导致识别器之一被忽略。在这种情况下,似乎忽略了垂直手势,而倾向于水平手势。

参考:https://github.com/flutter/flutter/blob/03a1f4acb315bd5cd99c5cafe19a4875f9f98422/packages/flutter/lib/src/widgets/gesture_detector.dart#L187

希望这可以为您解决问题:)

答案 2 :(得分:0)

尝试在创建您的GestureDetector和/或父组件时记录日志,并查看是否在End事件之前将其重新呈现。我遇到了同样的问题,这是由于我使用的Update事件引起了重新渲染的副作用。在进行了一些修改以防止这种情况发生后,End事件开​​始起作用。

答案 3 :(得分:0)

看起来像GridView小部件正在捕获垂直拖动手势。可能不是最佳解决方案,但是通过将GestureDetector小部件作为EACH GridView子级的父级,我能够完成这项工作。