onNavigate method

Future<SilentActionResult> onNavigate(
  1. NodeSpec action,
  2. ActionContext context
)

Implementation

Future<SilentActionResult> onNavigate(
    NodeSpec action, ActionContext context) async {
  final props = action.props;
  final screen =
      Schema.getScreen(props["jumpToScreen"] ?? props["jumpToRoute"]);
  final screenState = props["state"];

  if (screen != null) {
    Route route;
    final transition =
        Types.routeTransitionBuilder.build(props["transition"]);
    if (transition == null) {
      route = Lowder.widgets.buildRoute(screen, state: screenState);
    } else {
      route = PageRouteBuilder(
        settings: RouteSettings(name: screen.name, arguments: screen),
        pageBuilder: (context, animation, secondaryAnimation) =>
            Lowder.widgets.buildScreen(context, screen, state: screenState),
        transitionsBuilder: transition,
        transitionDuration: Duration(
            milliseconds:
                parseInt(props["transitionDuration"], defaultValue: 200)),
      );
    }

    tailFunc(value) {
      final onPopAction = action.actions["onPop"];
      if (onPopAction != null && context.buildContext.mounted) {
        Lowder.actions.run(
          context.buildContext,
          NodeSpec.fromMap(onPopAction),
          context.state,
          value,
          context.actionContext,
        );
      }
    }

    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      final navigator = Lowder.actions.appNavigator;
      if (parseBool(props["replacePrevious"])) {
        if (navigator.canPop()) {
          navigator.pushReplacement(route).then(tailFunc);
        } else {
          navigator.push(route).then(tailFunc);
        }
      } else if (parseBool(props["replaceAll"])) {
        navigator.popUntil((route) => route.isFirst);
        if (!Lowder.editorMode) {
          navigator.pushReplacement(route).then(tailFunc);
        } else {
          navigator.push(route).then(tailFunc);
        }
      } else {
        navigator.push(route).then(tailFunc);
      }
    });
    SchedulerBinding.instance.ensureVisualUpdate();
  }
  return SilentActionResult(true);
}