![](/img/trans.png)
[英]flutter - android back button does not call onWillPop of WillPopScope
[英]Flutter iOS Back Swipe does not call onWillPop
我用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...
],
),
),
);
}
我找到了原因:WillPopScope 不起作用,因为上面构建方法的widget 不是top widget(main 调用的widget)。 我希望它可以帮助其他人。
正如本文所提到这里<\/a>,让你的工作WillPopScope,<\/strong>你应该重写hasScopedWillPopCallback<\/strong>吸气剂MaterialPageRoute<\/strong>这样的:
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,
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.