Dart 检测颤振中的垂直滑动方向

Dart 检测颤振中的垂直滑动方向,dart,flutter,flutter-layout,Dart,Flutter,Flutter Layout,如何检测用户何时垂直向上或向下滑动 我一直在使用这个包,但现在,它给了我一些例外,比如 The getter 'globalPosition' was called on null. 导入“包装:颤振/材料.省道”; 类SwipedTectorExample扩展StatefulWidget{ 最后一个函数()是WIPEUP; 最后一个函数()用于删除; 最后一个孩子; SwipedTectorExample({this.onSwipeUp,this.onSwipeDown,this.child

如何检测用户何时垂直向上或向下滑动

我一直在使用这个包,但现在,它给了我一些例外,比如

The getter 'globalPosition' was called on null.
导入“包装:颤振/材料.省道”;
类SwipedTectorExample扩展StatefulWidget{
最后一个函数()是WIPEUP;
最后一个函数()用于删除;
最后一个孩子;
SwipedTectorExample({this.onSwipeUp,this.onSwipeDown,this.child});
@凌驾
_SwipeDetectorExampleState createState()=>\u SwipeDetectorExampleState();
}
类_SwipedTectorExampleState扩展状态{
//垂直拖动详细信息
DragStartDetails startVerticalDragDetails;
DragUpdateDetails updateVerticalDragDetails;
@凌驾
小部件构建(构建上下文){
返回手势检测器(
垂直排水起点:(排水详情){
startVerticalDragDetails=dragDetails;
},
垂直排水更新:(排水详情){
updateVerticalDragDetails=dragDetails;
},
垂直排水量:(endDetails){
double dx=updateVerticalDragDetails.globalPosition.dx-
startVerticalDragDetails.globalPosition.dx;
double dy=updateVerticalDragDetails.globalPosition.dy-
startVerticalDragDetails.globalPosition.dy;
双速度=endDetails.primaryVelocity;
//将值转换为正值
如果(dx<0)dx=-dx;
如果(dy<0)dy=-dy;
如果(速度<0){
onswipup();
}否则{
widget.onsweedown();
}
},
child:widget.child);
}
}

我今天遇到了同样的问题,请尝试运行
颤振清理
,然后重新启动应用程序。 如果这没有帮助,人们可以像阿里先生回答的那样使用手势检测器。在我的例子中,简单的热重启修复了问题。

您可能正在寻找。我不确定SwipedTector包为什么会存在,因为我可以发誓GestureDetector已经存在很长时间了,但无论如何,存储库似乎已经不存在了。看看这个答案:
import 'package:flutter/material.dart';

class SwipeDetectorExample extends StatefulWidget {
  final Function() onSwipeUp;
  final Function() onSwipeDown;
  final Widget child;

  SwipeDetectorExample({this.onSwipeUp, this.onSwipeDown, this.child});

  @override
  _SwipeDetectorExampleState createState() => _SwipeDetectorExampleState();
}

class _SwipeDetectorExampleState extends State<SwipeDetectorExample> {
  //Vertical drag details
  DragStartDetails startVerticalDragDetails;
  DragUpdateDetails updateVerticalDragDetails;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onVerticalDragStart: (dragDetails) {
          startVerticalDragDetails = dragDetails;
        },
        onVerticalDragUpdate: (dragDetails) {
          updateVerticalDragDetails = dragDetails;
        },
        onVerticalDragEnd: (endDetails) {
          double dx = updateVerticalDragDetails.globalPosition.dx -
              startVerticalDragDetails.globalPosition.dx;
          double dy = updateVerticalDragDetails.globalPosition.dy -
              startVerticalDragDetails.globalPosition.dy;
          double velocity = endDetails.primaryVelocity;

          //Convert values to be positive
          if (dx < 0) dx = -dx;
          if (dy < 0) dy = -dy;

          if (velocity < 0) {
            widget.onSwipeUp();
          } else {
            widget.onSwipeDown();
          }
        },
        child: widget.child);
  }
}