generator method

RouteFactory generator({
  1. Widget builder(
    1. BuildContext context,
    2. Widget widget
    )?,
})

Generates the RouteFactory which builds a Route on request.

These routes are built using the NyRouterRoutes addRoute method.

Implementation

RouteFactory generator(
    {Widget Function(BuildContext context, Widget widget)? builder}) {
  return (RouteSettings settings) {
    if (settings.name == null) return null;

    Uri? uriSettingName;
    bool routeNamedArg = isRouteNamedArg(settings.name!);
    if (!routeNamedArg) {
      try {
        uriSettingName = Uri.parse(settings.name!);
      } on FormatException catch (e) {
        NyLogger.error(e.toString());
      }
    }

    String routeName = settings.name!;
    if (uriSettingName != null) {
      routeName = uriSettingName.path;
    }

    ArgumentsWrapper? argumentsWrapper;
    if (settings.arguments is ArgumentsWrapper) {
      argumentsWrapper = settings.arguments as ArgumentsWrapper?;
    } else {
      argumentsWrapper = ArgumentsWrapper();
      argumentsWrapper.baseArguments = NyArgument(settings.arguments);
    }

    if (argumentsWrapper?.baseArguments?.data != null &&
        argumentsWrapper?.prefix != null) {
      String prefix = argumentsWrapper!.prefix!;
      routeName = routeName.replaceFirst(prefix, "");
    }

    argumentsWrapper ??= ArgumentsWrapper();

    if (uriSettingName != null && uriSettingName.queryParameters.isNotEmpty) {
      argumentsWrapper.queryParameters =
          NyQueryParameters(uriSettingName.queryParameters);
    }

    if (routeNamedArg) {
      final matcher = RouteMatcher(settings.name!);
      final match = matcher.findMatch(_routeNameMappings);

      if (argumentsWrapper.queryParameters != null) {
        Map<String, String> mappedData =
            Map.from(argumentsWrapper.queryParameters!.data);
        mappedData.addAll(match?.parameters ?? {});
        argumentsWrapper.queryParameters = NyQueryParameters(mappedData);
      } else {
        argumentsWrapper.queryParameters =
            NyQueryParameters(match?.parameters ?? {});
      }

      routeName = match?.pattern ?? routeName;
    }

    final NyArgument? baseArgs = argumentsWrapper.baseArguments;
    final NyQueryParameters? queryParameters =
        argumentsWrapper.queryParameters;

    argumentsWrapper.pageTransitionSettings ??=
        const PageTransitionSettings();

    late final NyRouterRoute? route;

    if (routeName == "/") {
      route = _routeNameMappings[routeName];
    } else {
      if (_routeNameMappings.containsKey(routeName)) {
        route = _routeNameMappings[routeName];
      } else {
        if (_routeUnknownMappings.isNotEmpty) {
          route = _routeUnknownMappings.entries.first.value;
        }
      }
    }

    if (route == null) return null;

    route.pageTransitionSettings ??= const PageTransitionSettings();

    if (Nylo.instance.onDeepLinkAction != null) {
      Nylo.instance.onDeepLinkAction!(route.name, queryParameters?.data);
    }

    return PageTransition(
      child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
        if (builder != null) {
          Widget widget = route!.builder(
            context,
            baseArgs ?? route.defaultArgs,
            queryParameters ?? route.queryParameters,
          );
          return builder(context, widget);
        }
        return route!.builder(context, baseArgs ?? route.defaultArgs,
            queryParameters ?? route.queryParameters);
      }),
      type: argumentsWrapper.pageTransitionType ??
          (route.pageTransitionType ?? PageTransitionType.rightToLeft),
      settings: settings,
      duration: _getPageTransitionDuration(route, argumentsWrapper),
      alignment: _getPageTransitionAlignment(route, argumentsWrapper),
      reverseDuration:
          _getPageTransitionReversedDuration(route, argumentsWrapper),
      childCurrent: _getPageTransitionChildCurrent(route, argumentsWrapper),
      curve: _getPageTransitionCurve(route, argumentsWrapper),
      ctx: _getPageTransitionContext(route, argumentsWrapper),
      inheritTheme: _getPageTransitionInheritTheme(route, argumentsWrapper),
      fullscreenDialog:
          _getPageTransitionFullscreenDialog(route, argumentsWrapper),
      opaque: _getPageTransitionOpaque(route, argumentsWrapper),
    );
  };
}