Android 在 Flutter 中弹出另一个页面后,后退按钮不调用 onWillPop

Android back button is not calling onWillPop after popping another page in Flutter

我有一个 BottomNavigationBar 和一个 IndexedStack,我正在尝试在每个导航选项卡中实现嵌套导航。例如,“搜索”选项卡有自己的 Navigator 包裹在 WillPopScope 中,如下所示:

WillPopScope(
  onWillPop: () async {
    BottomNavigationBar navigationBar = navigationKey.currentWidget;
    if (Navigator.canPop(navigatorContext)) {
      Navigator.pop(navigatorContext);
    } else if (searchBarController.isOpen) {
      searchBarController.close();
    } else {
      BottomNavigationBar navigationBar = navigationKey.currentWidget;
      navigationBar.onTap(0);
    }
    return false;
  },
  child: Navigator(
    onGenerateRoute: (settings) {
      return MaterialPageRoute(
        settings: settings,
        builder: (context) {
          switch (settings.name) {
            case '/':
              return buildRootPage();
            case '/movie':
              return MoviePage();
            default:
              return null;
          }
        },
      );
    },
  ),
);

将页面推送到此搜索选项卡并弹出后,后退按钮不再调用 onWillPop 方法并且没有任何反应。但是,每当我点击屏幕上的任何地方时,后退按钮又开始工作了。

这可能是什么原因造成的?

确保 WillPopScope 小部件是顶级小部件,而不是 MaterialApp 或 Scaffold 的子部件