I covered my Scaffold
with a WillPopScope
, but the onWillPop
callback is not called when swiping to go back on iOS.
What could be the reason for this?
@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...
],
),
),
);
}
I found the reason: WillPopScope does not work because the widget with the above build method is not a top widget (the widget called by main). I hope it helps others.
this is a problem and discussed a lot in this issue but there is a way to fix it and it is to use this package
return ConditionalWillPopScope(
child: _MyScreenContent(),
onWillPop: _onWillPop,
shouldAddCallbacks: _hasChanges,
);
As it is mentioned here here<\/a> , to make your WillPopScope<\/strong> work, you should override hasScopedWillPopCallback<\/strong> getter in MaterialPageRoute<\/strong> like this:
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,
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.