buildRoute method

  1. @override
VRoute? buildRoute(
  1. VPathRequestData vPathRequestData, {
  2. required VPathMatch parentVPathMatch,
  3. required bool parentCanPop,
})
inherited

This is basically the same as VPath.buildRoute except that we add the page of this VRouteElement as a page to VRoute.pages

Implementation

@override
VRoute? buildRoute(
  VPathRequestData vPathRequestData, {
  required VPathMatch parentVPathMatch,
  required bool parentCanPop,
}) {
  // Set localPath to null since a VRouteElementWithPage marks a limit between localPaths
  VPathMatch newVPathMatch = (parentVPathMatch is ValidVPathMatch)
      ? ValidVPathMatch(
          remainingPath: parentVPathMatch.remainingPath,
          pathParameters: parentVPathMatch.pathParameters,
          localPath: null,
          names: parentVPathMatch.names + [if (name != null) name!],
        )
      : InvalidVPathMatch(
          localPath: null,
          names: parentVPathMatch.names + [if (name != null) name!],
        );

  VRoute? childVRoute;
  for (var vRouteElement in stackedRoutes) {
    childVRoute = vRouteElement.buildRoute(
      vPathRequestData,
      parentVPathMatch: newVPathMatch,
      parentCanPop: true,
    );
    if (childVRoute != null) {
      break;
    }
  }

  final bool validParentVRoute = !(parentVPathMatch is InvalidVPathMatch) &&
      (parentVPathMatch as ValidVPathMatch).remainingPath.isEmpty;
  if (childVRoute == null && !validParentVRoute) {
    return null;
  }

  final VRouteElementNode vRouteElementNode = VRouteElementNode(
    this,
    localPath: null,
    stackedVRouteElementNode: childVRoute?.vRouteElementNode,
  );

  Map<String, String> pathParameters = childVRoute?.pathParameters ??
      (parentVPathMatch as ValidVPathMatch).pathParameters;

  return VRoute(
    vRouteElementNode: vRouteElementNode,
    pages: [
      pageBuilder(
        key ?? ValueKey(parentVPathMatch.localPath),
        Builder(
          builder: (context) => LocalVRouterData(
            child: NotificationListener<VWidgetGuardMessage>(
              // This listen to [VWidgetGuardNotification] which is a notification
              // that a [VWidgetGuard] sends when it is created
              // When this happens, we store the VWidgetGuard and its context
              // This will be used to call its afterUpdate and beforeLeave in particular.
              onNotification: (VWidgetGuardMessage vWidgetGuardMessage) {
                VWidgetGuardMessageRoot(
                  vWidgetGuardState: vWidgetGuardMessage.vWidgetGuardState,
                  localContext: vWidgetGuardMessage.localContext,
                  associatedVRouteElement: this,
                ).dispatch(vPathRequestData.rootVRouterContext);

                return true;
              },
              child: widget,
            ),
            vRouteElementNode: vRouteElementNode,
            url: vPathRequestData.url,
            previousUrl: vPathRequestData.previousUrl,
            historyState: vPathRequestData.historyState,
            pathParameters: pathParameters,
            queryParameters: vPathRequestData.queryParameters,
            context: context,
          ),
        ),
        name ?? parentVPathMatch.localPath,
      ),
      ...childVRoute?.pages ?? []
    ],
    pathParameters: pathParameters,
    vRouteElements:
        <VRouteElement>[this] + (childVRoute?.vRouteElements ?? []),
    names: (childVRoute?.names ?? []) + [if (name != null) name!],
  );
}