Flutter滑动手势测试中无法检测到速度值的问题求助
Flutter滑动手势测试中无法检测到速度值的问题求助
各位大佬好!我最近在测试Flutter组件的水平滑动手势逻辑时碰到了一个棘手的问题,想来请教下大家的解决思路。
我在组件里用了GestureDetector来监听水平滑动,核心逻辑是通过onHorizontalDragEnd回调里的DragEndDetails.primaryVelocity来判断滑动方向,代码如下:
GestureDetector( onHorizontalDragEnd: (DragEndDetails details) { if (details.primaryVelocity > 0) { // 向右滑动 } else if (details.primaryVelocity < 0) { // 向左滑动 } } );
为了测试这段逻辑,我写了对应的Widget测试代码:
var widget = MyWidgetWithTheGestureDetectorInside(); await tester.pumpWidget(widget); TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(GestureDetector))); await gesture.moveBy(const Offset(100, 0)); await gesture.up(); await tester.pumpAndSettle();
我还试过用await tester.drag(...)来替代手动创建TestGesture的方式,但结果完全一样:details.primaryVelocity始终是0,details.velocity的所有属性也都是0,就像完全没有滑动过一样。
但让我困惑的是,当我在模拟器里手动操作滑动时,primaryVelocity能正常返回对应方向的数值,完全能正确判断滑动方向。
我实在找不到问题出在哪了,有没有大佬能帮我看看我到底哪里写错了?
备注:内容来源于stack exchange,提问作者identify
相关产品推荐
相关产品推荐

