popRoute method

  1. @override
Future<bool> popRoute()
override

Called by the Router when the Router.backButtonDispatcher reports that the operating system is requesting that the current route be popped.

The method should return a boolean Future to indicate whether this delegate handles the request. Returning false will cause the entire app to be popped.

Consider using a SynchronousFuture if the result can be computed synchronously, so that the Router does not need to wait for the next microtask to schedule a build.

Implementation

@override
Future<bool> popRoute() async {
  // 首先检查当前的Navigator是否可以pop(例如,一个对话框或底部面板)
  if (Navigator.of(navigatorKey.currentContext!).canPop()) {
    Navigator.of(navigatorKey.currentContext!).pop();
    return Future.value(true);
  }
  // 如果不行,则尝试从我们自己的页面栈中pop一个页面
  if (canPop()) {
    _pages.removeLast();
    notify();
    return Future.value(true);
  }

  /// 如果页面栈也不能pop,则执行自定义的退出逻辑
  return _exitWindowStyle == null
      ? Future.value(false)
      : _exitWindowStyle!.call(navigatorKey.currentContext!);
}