You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 15:22:32