![](/img/trans.png)
[英]Flutter- GestureDetector detect the direction of Horizontal and Vertical Drag
[英]Detect vertical swipe direction in Flutter
如何檢測用戶何時垂直向上或向下滑動?
我一直在使用swipedetector包,但現在,它給了我例外
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 clean
,然后熱重啟你的應用程序。 如果這沒有幫助,您可以隨時使用手勢檢測器作為 M.Ali 的回答。 在我的情況下,一個簡單的熱重啟解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.