首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter iOS Back Swipe不调用onWillPop

Flutter iOS Back Swipe不调用onWillPop
EN

Stack Overflow用户
提问于 2019-08-05 18:38:26
回答 3查看 1.4K关注 0票数 5

我用WillPopScope覆盖了我的Scaffold,但在iOS上滑动返回时不会调用onWillPop回调。

这可能是什么原因呢?

代码语言:javascript
运行
复制
@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: () async {
      print("onWillPop"); // is not printed to the console
      if (Navigator.of(context).userGestureInProgress)
        return false;
      else
        return true;
    },
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text("App Title"),
      ),
      body: Stack(
        children: <Widget>[
          ...widgets...
        ],
      ),
    ),
  );
}
EN

回答 3

Stack Overflow用户

发布于 2022-01-25 11:16:11

正如前面提到的getter,为了让你的WillPopScope工作,你应该像这样覆盖MaterialPageRoute中的hasScopedWillPopCallback getter:

代码语言:javascript
运行
复制
class CustomMaterialPageRoute extends MaterialPageRoute {
  @protected
  bool get hasScopedWillPopCallback {
    return false;
  }
  CustomMaterialPageRoute({
    @required WidgetBuilder builder,
    RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
  }) : super(
          builder: builder,
          settings: settings,
          maintainState: maintainState,
          fullscreenDialog: fullscreenDialog,
        );
}

然后在您的路由器中更换CustomMaterialPageRoute上的MaterialPageRoute。这肯定会让WillPopScope在IOS和Android平台上都能工作。

票数 1
EN

Stack Overflow用户

发布于 2019-08-05 19:19:42

我找到了原因: WillPopScope无法工作,因为使用上述构建方法的小部件不是顶级小部件(由main调用的小部件)。我希望它能帮助其他人。

票数 0
EN

Stack Overflow用户

发布于 2021-09-08 09:55:19

这是一个问题,在本issue中讨论了很多,但是有一种方法可以解决它,那就是使用this package

代码语言:javascript
运行
复制
return ConditionalWillPopScope(
child: _MyScreenContent(),
onWillPop: _onWillPop,
shouldAddCallbacks: _hasChanges,
 );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57356879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档