在Flutter中检测垂直滑动方向 [英] Detect vertical swipe direction in Flutter
本文介绍了在Flutter中检测垂直滑动方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检测用户何时垂直向上或向下滑动?
How to detect when the user swipe vertically either upward on downward?
我一直在使用 swipedetector 包,但是现在,它给了我
I have been using swipedetector package, but now, it gives me exceptions like
The getter 'globalPosition' was called on null.
推荐答案
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);
}
}
这篇关于在Flutter中检测垂直滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文