在Flutter中检测垂直滑动方向

在Flutter中检测垂直滑动方向,第1张

在Flutter中检测垂直滑动方向
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);  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5016620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存