杀扑手势

Killing flutter gesture

我正在尝试在满足特定条件时阻止手势继续。

场景是用户正在滑动标签栏,如果他们转到下一个标签并且条件为真,则应该禁用进一步滑动。我试图将标签栏放在一个堆栈中,顶部有一个 Absorb 指针容器,但如果他们不放开原始手势(即他们到达新标签但没有放开屏幕)它仍然允许他们拖动它。

有没有办法停止原来的滑动手势?

我找到了这个取消方法,但我不知道如何访问它

https://api.flutter.dev/flutter/gestures/Drag/cancel.html

在您的情况下,无需处理 TabBarView 之外的手势,只需更改 ScrollPhysics

这是一个代码示例:

final isDisable = ValueNotifier(false);

tabController.animation?.addListener(() {

  //The scrolling will be disabled if the second tab is displayed so you can 
  //add your own logic, may be just checking tabController.index

  if (tabController.animation!.value == 1.0) {
    isDisable.value = true;
  }
});

return Scaffold(
  body: ValueListenableBuilder(
    valueListenable: isDisable,
    builder: (context, bool value, child) => TabBarView(
      controller: tabController,

      // changing the physics to NeverScrollableScrollPhysics will disable scrolling
      physics: value
          ? NeverScrollableScrollPhysics()
          : AlwaysScrollableScrollPhysics(),

      children: children,
    ),
  ),
);