Flutter iOS Back Swipe 不调用 onWillPop

ahm*_* ay 7 dart flutter

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

@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...
        ],
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

Rus*_*809 3

正如此处提到的,为了使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,
        );
}
Run Code Online (Sandbox Code Playgroud)

然后在Router中替换CustomMaterialPageRoute上的MaterialPageRoute。它肯定会让WillPopScope同时适用于 IOS 和 Android 平台。