build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Called by the Router to obtain the widget tree that represents the current state.

This is called whenever the Futures returned by setInitialRoutePath, setNewRoutePath, or setRestoredRoutePath complete as well as when this notifies its clients (see the Listenable interface, which this interface includes). In addition, it may be called at other times. It is important, therefore, that the methods above do not update the state that the build method uses before they complete their respective futures.

Typically this method returns a suitably-configured Navigator. If you do plan to create a navigator, consider using the PopNavigatorRouterDelegateMixin. If state restoration is enabled for the Router using this delegate, consider providing a non-null Navigator.restorationScopeId to the Navigator returned by this method.

This method must not return null.

The context is the Router's build context.

Implementation

@override
Widget build(BuildContext context) {
  if (pageTrack.isEmpty) {
    var defaultPage = appRoutePath.currentPage!;
    registerTrackIndex(defaultPage);
  }
  // 加载首页时,回调before周期
  if (before != null && !_isCallBefore) {
    _isCallBefore = !_isCallBefore;
    String currentRoute = pageTrackIndexMapRoute[pageTrack.length - 1]!;
    firstTimeBeforeCall = before!(appRoutePath.getRoutePageByRoute(currentRoute));
    isCallBefore = true;
    return beforeCallback();
  } else if (isCallBefore) {
    return beforeCallback();
  }
  // 入栈新路由
  if (pushPageInfo != null) {
    registerTrackIndex(pushPageInfo!);
    pushPageInfo = null;
  }
  // 通过AppRoute before hook更换当前最新的页面
  if (replaceCurrentPageInfo != null) {
    pageTrack.removeLast();
    registerTrackIndex(replaceCurrentPageInfo!);
    replaceCurrentPageInfo = null;
  }
  // 浏览器url跳转处理
  if (appRoutePath.getJumpRoute() != null) {
    String jumpRoute = appRoutePath.getJumpRoute()!;
    var currentPageIndex = pageTrack.length - 1;
    var currentRoute = pageTrackIndexMapRoute[currentPageIndex];
    final void Function(String route) pushNewRoute = (String route) {
      final newPage = appRoutePath.getRoutePageByRoute(route);
      registerTrackIndex(newPage);
    };
    // 大于路栈大于1,则当前的系统提供的url可能是回退或前进
    if (pageTrack.length > 1) {
      String preRoute = pageTrackIndexMapRoute[pageTrack.length - 2]!;
      // 判定为回退操作路由
      if (preRoute == currentRoute) {
        pageTrack = pageTrack.sublist(0, preRoute.length - 2);
      } else {
        pushNewRoute(jumpRoute);
      }
      // 即不是前进或后退,又不是一样的路由,那主判定新页面路由
    } else if (currentRoute != jumpRoute) {
      pushNewRoute(jumpRoute);
    }
    appRoutePath.cleanJumpRoute();
  }

  return Navigator(
    key: navigatorKey,
    pages: pageTrack,
    onPopPage: handlePopPage,
  );
}