简体   繁体   English

Flutter- GestureDetector 检测水平和垂直拖动的方向

[英]Flutter- GestureDetector detect the direction of Horizontal and Vertical Drag

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

Did you try onPanUpdate(details) method?您是否尝试过onPanUpdate(details)方法? Here is how you can do it.这是您如何做到的。

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");
});

Note: This callback causes a crash if onHorizontalDragUpdate() or onVerticalDragUpdate() is set as mentioned by anmol.majhail.注意:如果onHorizontalDragUpdate()onVerticalDragUpdate()设置为 anmol.majhail 所述,则此回调会导致崩溃。

For those, who have " Having both a pan gesture recognizer and a scale gesture recognizer is redundant; scale is a superset of pan ."对于那些“同时拥有平移手势识别器和缩放手势识别器是多余的;缩放是平移的超集”的人来说。 error:错误:

Implementing both drag and zoom-in with [onScaleUpdate] in [GestureDetector] widget:在 [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);
    });
  }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM