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...
        ],
      ),
    ),
  );
}
As it is mentioned here, to make your WillPopScope work, you should override hasScopedWillPopCallback getter in MaterialPageRoute 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,
        );
}
And then replace MaterialPageRoute on CustomMaterialPageRoute in your Router. It definitely make WillPopScope work for both IOS and Android platforms for sure.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With