![](/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.