首页 > 解决方案 > 在 WillPopScope 中设置 `onWillPop` 方法时如何启用向后滑动效果

问题描述

如flutter源码所写。设置onWillPop方法后,滑动效果被禁用。

static bool _isPopGestureEnabled<T>(PageRoute<T> route) {
  ...

  // If attempts to dismiss this route might be vetoed such as in a page
  // with forms, then do not allow the user to dismiss the route with a swipe.
  if (route.hasScopedWillPopCallback)
    return false;

  ...

}

这似乎是在issue中讨论的预期行为。

那么,我该如何收听 Navigator.pop 事件。我的需要是,如果某些变量不正确,它就无法成功返回。

标签: flutter

解决方案


我刚刚测试了创建一个自定义 RouteManager 并且 willPop 在执行回扫效果时永远不会被调用。另一方面,您可以在删除 Widget 之前调用dispose()deactivate()在您的 State 上清理您的变量。


推荐阅读