unknownRoute method

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

Generates the RouteFactory for unknown routes.

Implementation

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

    Uri? uriSettingName;
    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);
    }

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

    argumentsWrapper.pageTransitionSettings ??=
        const PageTransitionSettings();

    if (_routeUnknownMappings.isEmpty) {
      return null;
    }

    final NyRouterRoute route = _routeUnknownMappings.entries.first.value;
    route.name = routeName;

    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),
    );
  };
}