在我的应用中,当用户向下拖动时,我想执行一个方法。问题是,即使用户仅在屏幕上点击,也会触发VerticalDragDown事件。我的猜测是轻击事件触发了距离很短的拖动事件。因此,我想计算用户拖动的距离,如果该距离大于一定量,则调用该方法。
令人惊讶的是,在DragStartDetails,DragEndDetails,DragDownDetails类中没有此类信息。 details.globalPosition.distance属性似乎是从触摸点到屏幕边缘的距离,而不是拖动距离,因为我尝试用不同的长度拖动,但结果几乎相同。而且仅调用了onVerticalDragDown回调。
这是我的代码:
Widget _buildScanGuide() {
return GestureDetector(
onVerticalDragStart: (details) {
print('DragStart: $details');
},
onVerticalDragDown: (details) {
print('DragDown: $details');
},
onVerticalDragEnd: (details) {
print('DragEnd: $details');
},
onVerticalDragUpdate: (details) {
print('DragUpdate: $details');
},
child: ListView(
padding: EdgeInsets.only(top: 50.0),
children: <Widget>[
Center(
child: Text('No device founded!',
style: TextStyle(fontSize: 24.0, color: kTextColor))),
Padding(
padding: const EdgeInsets.symmetric(vertical: 40.0),
child: Icon(
Icons.arrow_downward,
size: 96.0,
color: kTextColor,
),
),
Center(
child: Text('Drag down to scan for devices.',
style: TextStyle(fontSize: 24.0, color: kTextColor))),
],
),
);
}
在调试控制台中:
I/flutter (20427): DragDown: DragDownDetails(Offset(268.0, 192.5))
I/flutter (20427): DragDown: DragDownDetails(Offset(288.4, 173.2))
I/flutter (20427): DragDown: DragDownDetails(Offset(92.7, 235.4))
I/flutter (20427): DragDown: DragDownDetails(Offset(264.5, 168.1))
I/flutter (20427): DragDown: DragDownDetails(Offset(264.5, 173.2))
I/flutter (20427): DragDown: DragDownDetails(Offset(280.3, 176.6))
I/flutter (20427): DragDown: DragDownDetails(Offset(267.6, 178.8))
I/flutter (20427): DragDown: DragDownDetails(Offset(63.9, 250.6))
I/flutter (20427): DragDown: DragDownDetails(Offset(277.8, 191.4))
I/flutter (20427): DragDown: DragDownDetails(Offset(279.2, 206.9))
我正在使用Flutter v0.5.7开发者频道。
答案 0 :(得分:0)
我不得不使用不推荐使用的Listener,但是它解决了问题
return Listener(
onPointerDown: (details) {
_pointerDownPosition = details.position;
},
onPointerUp: (details) {
if (details.position.dy - _pointerDownPosition.dy > 50.0) {
_dragHandler(pf10Bloc);
}
},
child: //...
更多详细信息可以在文档https://flutter.io/gestures/
中找到答案 1 :(得分:0)
onVerticalDragDown
回调在以下时间被调用
指针已接触屏幕并且可能开始垂直移动。
如果要检测垂直拖动,建议您处理onVerticalDragUpdate
回调。 DragUpdateDetails
参数包含一个primaryDelta
属性,即:
自从开始,指针沿主轴移动的量。 以前的更新。
如果仅调用onVerticalDragDown
,我认为是因为ListView
捕获了要滚动的垂直拖动事件。
在您的示例中,如果不需要滚动功能,则应使用Column
小部件,否则,您将不得不收听滚动位置的更新。