【问题标题】:Flutter- GestureDetector detect the direction of Horizontal and Vertical DragFlutter- GestureDetector 检测水平和垂直拖动的方向
【发布时间】:2019-01-17 15:09:07
【问题描述】:

我正在使用GestureDetector,但没有找到任何告诉您拖动方向的onXYZ 事件。

【问题讨论】:

  • @CopsOnRoad 回答 - onPanUpdate - 与屏幕接触并在垂直或水平方向移动的指针。如果设置了 onHorizo​​ntalDragUpdate 或 onVerticalDragUpdate,此回调会导致崩溃。
  • @anmol.majhail 感谢您指出这一点,让我更新我的答案。
  • @anmol.majhail 我注意到当我同时使用onPanUpdate()onHorizontalDragUpdate() 时没有崩溃,但是最初后者收到更新,一段时间后,前一个回调开始更新,后来停止。你能告诉我你在哪里发现我们不应该同时使用这两个回调吗?

标签: flutter dart flutter-layout


【解决方案1】:

你试过onPanUpdate(details)方法吗?这是你可以做到的。

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");
  },
  child: Container(
    color: Colors.blue,
    width: double.infinity,
    height: double.infinity,
  ),
)

注意:如果onHorizontalDragUpdate()onVerticalDragUpdate() 也如 anmol.majhail 所述提供,则此回调会导致崩溃。

【讨论】:

  • onPanUpdate 不起作用..我添加了一个打印行,它只执行一次..你知道为什么吗?
【解决方案2】:

对于那些“同时拥有平移手势识别器和缩放手势识别器是多余的;缩放是平移的超集”的人。错误:

在 [GestureDetector] 小部件中使用 [onScaleUpdate] 实现拖动和放大:

onScaleUpdate: (scaleInfo) {
  //for drag, use [scaleInfo.focalPointDelta.dx] and [scaleInfo.focalPointDelta.dy]
  _controller.translate(scaleInfo.focalPointDelta.dx, scaleInfo.focalPointDelta.dy);
  
  //for zooming use [scaleInfo.scale] and don't react when zoom is exactly '1' [scaleInfo.scale != 1]
  if (scaleInfo.scale > 0.5 && scaleInfo.scale < 2 && scaleInfo.scale != 1) {
    setState(() {
      currentSize = Size(widget.size.width * scaleInfo.scale, widget.size.height * scaleInfo.scale);
    });
  }
},

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2019-09-11
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多