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) {
  return Navigator(
      key: navigatorKey,
      onPopPage: (route, result) {
        PlusRoute? currentRoute = this.state._router.route;
        PlusRoute? defaultRoute = this.state._router.defaultRoute;
        // Don't back when is initialRoute
        if (currentRoute == defaultRoute) {
          route.didPop(false);
          return false;
        } else {
          // Check route can back
          if (!route.didPop(result)) return false;

          // Back with state back()
          return this.state.back();
        }
      },
      pages: [
        MaterialPage(
            key: ValueKey("plus_router_init_page"),
            child: loadPage ?? PlusRouterLoadPage()),

        // Activated Route
        for (PlusRoute route in state._router.activatedRoutes)
          MaterialPage(
              key: ValueKey(route.name),
              child: route.widget ?? route.builder!(state, route.arguments)),

        // current route
        if (state.currentRoute != null)
          MaterialPage(
              key: ValueKey(state.currentRoute!.name),
              child: state.currentRoute!.widget ??
                  state.currentRoute!.builder!(
                      state, state.currentRoute!.arguments)),
      ]);
}