build method
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) {
logger.d('build executed');
if (startPath != null) handler.routePath = startPath!;
startPath = null;
logger.d('handler.routeName: ${handler.routePath?.routeName}');
final routePath = handler.routePath ?? RoutePath();
_routePath = routePath;
// initiate browser title
SystemChrome.setApplicationSwitcherDescription(
ApplicationSwitcherDescription(
label: routePath.getRouteInstance.title,
primaryColor: 0,
));
pages.add(const MaterialPage(child: SizedBox.shrink()));
return Navigator(
key: navigatorKey,
pages: pages,
onPopPage: (route, result) {
final oldRoutePath = routePath.getRouteInstance;
logger.i('Pop Executed = ' + oldRoutePath.routePath);
logger.i('route.settings.name = ' + (route.settings.name ?? ''));
if (!route.didPop(result)) return false;
final newRoutePath = RoutePath(path: oldRoutePath.routePath);
_navigatorRoutePath = newRoutePath;
setNewRoutePath(newRoutePath);
return true;
},
onGenerateRoute: (setting) {
logger.i(
"onGenerateRoute RouteSettings: [name: ${setting.name ?? ''}, arguments: ${setting.arguments ?? ''}]");
if ((_routePath?.path ?? '-') == setting.name) {
logger.i('onGenerateRoute got a same path.');
return currentConfiguration.getRouteInstance.getPageRoute();
} else {
final newRouterPath = RoutePath(path: setting.name);
_navigatorRoutePath = newRouterPath;
setNewRoutePath(newRouterPath);
return newRouterPath.getRouteInstance.getPageRoute();
}
},
);
}