gpt4 book ai didi

Flutter iOS Back Swipe 不会调用 onWillPop

转载 作者:IT王子 更新时间:2023-10-29 07:02:11 27 4
gpt4 key购买 nike

我用 WillPopScope 覆盖了我的 Scaffold,但是在 iOS 上滑动返回时不会调用 onWillPop 回调。
这可能是什么原因?

@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...
],
),
),
);
}

最佳答案

如前所述here ,为了使您的 WillPopScope 正常工作,您应该像这样覆盖 MaterialPageRoute 中的 hasScopedWillPopCallback getter:

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,
);
}

然后在您的Router 中替换CustomMaterialPageRoute 上的MaterialPageRoute。它确实使 WillPopScope 可以同时适用于 IOS 和 Android 平台。

关于Flutter iOS Back Swipe 不会调用 onWillPop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57356879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com