screen function

void screen(
  1. dynamic screen,
  2. RouteMode mode, {
  3. dynamic argument,
  4. Symbol? routerName,
  5. BuildContext? context,
})

screen(): Function used by the core KareeRouter to launch screen on navigate.

Implementation

void screen(dynamic screen, RouteMode mode,
    {dynamic argument,
    Symbol? routerName,
    // RouteDirection direction = RouteDirection.LEFT_TO_RIGHT,
    cupertino.BuildContext? context}) {
  try {
    var isString = screen is String,
        isStls = screen is StatelessScreen,
        isStfs = screen is StatefulScreen,
        isRoutable = screen is RoutableWidget;
    if (!(isString || isStls || isStfs || isRoutable)) {
      throw NotManageableWidgetException(screen);
    }
    if (routerName != null && mode != RouteMode.INTERNAL) {
      throw BadUseOfRouterWidgetException(routerName, mode);
    }

    /// Internal Routing using RouterWidget.
    if (routerName != null) {
      Future.microtask(() => doInternalRouting(routerName, screen, argument));
      return;
    }
    switch (mode) {
      case RouteMode.REPLACE:
        if (screen is String) {
          KareeRouter.screenName = screen;
          KareeRouter.navigatorKey.currentState
              ?.pushReplacementNamed(screen, arguments: argument);
        } else {
          var settings = RouteSettings(
              arguments: argument, name: KareeRouter.currentRoute);
          KareeRouter.navigatorKey.currentState?.pushReplacement(
              cupertino.MaterialPageRoute(
                  builder: (_) => screen, settings: settings));
        }
        break;
      case RouteMode.PUSH:
        if (screen is String) {
          KareeRouter.screenName = screen;
          KareeRouter.navigatorKey.currentState
              ?.pushNamed(screen, arguments: argument);
        } else {
          var settings = RouteSettings(
              arguments: argument, name: KareeRouter.currentRoute);
          KareeRouter.navigatorKey.currentState?.push(
              cupertino.MaterialPageRoute(
                  builder: (_) => screen, settings: settings));
        }
        break;
      case RouteMode.EMPTY:
        if (screen is String) {
          KareeRouter.screenName = screen;
          KareeRouter.navigatorKey.currentState?.pushNamedAndRemoveUntil(
              screen, (_) => false,
              arguments: argument);
        } else {
          var settings = RouteSettings(
            name: KareeRouter.currentRoute,
            arguments: argument,
          );
          KareeRouter.navigatorKey.currentState?.pushAndRemoveUntil(
              cupertino.MaterialPageRoute(
                  builder: (_) => screen, settings: settings),
              (route) => false);
        }
        break;
      default:
        KareeRouter.navigatorKey.currentState?.pop(false);
    }
  } on NotManageableWidgetException catch (ex, st) {
    KareeRouter.goto(KareeConstants.kareeErrorPath, parameter: {
      #title: ex.message,
      #stack: st,
      #env: [screen, KareeRouter.currentRoute!, mode, argument],
      #errorCode: KareeErrorCode.notKareeScreen
    });
  } on BadUseOfRouterWidgetException catch (ex, st) {
    KareeRouter.goto(KareeConstants.kareeErrorPath, parameter: {
      #title: ex.message,
      #stack: st,
      #env: [
        '#${routerName.toString().substring(8, routerName.toString().length - 2)}',
        mode,
        KareeRouter.currentRoute!,
        argument
      ],
      #errorCode: KareeErrorCode.badUseOfRoutableWidget
    });
  }
}