Skip to main content

ark_navigation 2.1.0 copy "ark_navigation: ^2.1.0" to clipboard
ark_navigation: ^2.1.0 copied to clipboard

Typed modular Flutter navigation with explicit stack operations, deep links, nested scopes, and no code generation.

Ark Navigation #

Typed, modular navigation for Flutter applications, built directly on Flutter Router and Navigator.

Ark Navigation makes every destination and returned value part of a handwritten Dart contract. It does not require route generation, expose an untyped extra map, or hide stack mutations behind a generic navigation method.

Version 2.0.0 makes page construction independent of Material and Cupertino. Upgrading from 1.x requires migrating design-specific transitions to an application-owned page factory; the migration guide below includes complete code.

Why Ark Navigation #

  • Typed destinations, parameters, and route results.
  • Explicit push, replaceTop, replaceAll, pushAndRemoveUntil, replaceTail, popUntil, and branch operations.
  • Feature-owned modules that can be mounted into different applications without generated glue code.
  • Separate Redirect and Guard contracts, including enter and leave Guard phases.
  • Typed URI codecs, canonical links, decode-only aliases, and deterministic deep-link stack reconstruction.
  • Nested Navigator scopes with explicit outlets and branch state.
  • Browser-history intent attached to each operation.
  • Restoration, dynamic graph updates, multiple windows, and separate-engine integration boundaries.
  • No dependency on go_router, auto_route, code generation, a service locator, or another ArkTelos package.

Installation #

Build applications with a patched stable SDK: Dart 3.13.7+ or Flutter 3.47.8+. Package SDK constraints describe API compatibility, not the security of older runtimes. After the eMudhra incident, affected applications must be rebuilt and redistributed; updating a library alone is not enough. See the SDK security guidance.

dependencies:
  ark_navigation: ^2.1.0

Then import the public library:

import 'package:ark_navigation/ark_navigation.dart';

Page design in 2.0 #

The library no longer imports Material or Cupertino. Empty presentation means neutral, unanimated pages; fade, slide and custom animation remain available. Configure an application-owned NavigationPageFactory once on the Graph to use pages from material_ui, cupertino_ui, or another design system. Those design dependencies belong to the application, not to this package's runtime graph. The factory must preserve the supplied key, name, restorationId and child.

The page-design guide includes complete Material factory code, Cupertino setup, modal fallback, precedence and the 1.x migration. The runnable example is its executable counterpart.

Changing page animation does not bypass Guards or automatically enable native back gestures. The guide explains that boundary explicitly.

First route #

A destination is the typed request made by application code. Its type parameter is the value returned when that entry is popped.

final class HomeDestination extends NavigationDestination<void> {
  const HomeDestination();
}

final class ProductDestination extends NavigationDestination<bool> {
  const ProductDestination({required this.productId});

  final String productId;
}

A route definition connects one destination type to a page and, for restorable routes, a URI codec:

final NavigationRouteDefinition<ProductDestination, bool> productRoute =
    NavigationRouteDefinition<ProductDestination, bool>(
  id: const NavigationRouteId('product'),
  pageBuilder: (context, destination, entry) => ProductPage(
    productId: destination.productId,
  ),
  uriCodec: CallbackNavigationUriCodec<ProductDestination>(
    pattern: '/products/:productId',
    encoder: (destination) => Uri(
      pathSegments: <String>['products', destination.productId],
    ),
    decoder: (match) => ProductDestination(
      productId: match.path('productId'),
    ),
  ),
);

Routes belong to a module. A mount places that module at a URI prefix and in a Navigator scope:

final NavigationGraph graph = NavigationGraph(
  rootScopeId: const NavigationScopeId('root'),
  scopes: const <NavigationScopeDefinition>[
    NavigationScopeDefinition(
      id: NavigationScopeId('root'),
      restorationScopeId: 'root-navigation',
    ),
  ],
  modules: <NavigationModule>[
    NavigationModule(
      id: const NavigationModuleId('catalog'),
      routes: <NavigationRouteDefinitionBase>[homeRoute, productRoute],
    ),
  ],
  mounts: <NavigationMount>[
    NavigationMount(
      id: const NavigationMountId('catalog-root'),
      moduleId: const NavigationModuleId('catalog'),
      scopeId: const NavigationScopeId('root'),
    ),
  ],
);

Create one session for the Flutter view and give its Router configuration to the application:

final NavigationSession session = NavigationSession(
  id: const NavigationSessionId('main-window'),
  graph: graph,
);

MaterialApp.router(routerConfig: session.routerConfig);

The session must be disposed by the object that owns it. NavigationRuntime is available when several views or windows share one graph.

A destination already declares its input and pop-result type. Call the operation on that value; no generated helper or repeated result-type argument is needed:

final NavigationTicket<bool> ticket =
    const ProductDestination(productId: 'keyboard-42').push(context);

final NavigationOperationResult commit = await ticket.committed;
if (commit is! NavigationCommitted) {
  return; // A Guard rejected the request or resolution failed.
}

final NavigationCompletion<bool> completion = await ticket.completed;
if (completion case NavigationPopped<bool>(:final result)) {
  // result is bool?; Back may pop without a value.
}

For a button, the handler is simply:

onPressed: () {
  const ProductDestination(productId: 'keyboard-42').push(context);
},

After awaiting unrelated work, check context.mounted before using it. Destination methods reject an unmounted context and a context outside an ArkNavigationScope; there is no fallback global router.

The operation states exactly what changes #

Destination method Effect Return
push(context) Adds an entry NavigationTicket<R>
replaceTop(context) Removes the top entry and creates a new lifecycle NavigationTicket<R>
reconfigureTop(context) Updates a matching top entry, preserving its identity and ticket Future<NavigationOperationResult>
replaceAll(context) Makes this destination the only entry in its selected stack Future<NavigationOperationResult>
pushAndRemoveUntil(context, retain: matcher) Retains an anchor, removes its tail and pushes this destination NavigationTicket<R>

A rewrite completes removed entries as NavigationRemoved<R>, not as a successful typed pop. replaceAll does not clear every tab or session. Default browser history is push for push operations and replace for replacements; each method accepts an explicit historyMode.

Outside widgets: select an injected port #

A Presenter does not need a BuildContext. Its owner supplies the narrow NavigationPort, and the destination selects it explicitly:

final NavigationTicket<bool> ticket =
    const ProductDestination(productId: 'keyboard-42')
        .on(navigation)
        .push();

The returned NavigationDestinationActions<bool> is a non-owning target/port binding. It creates no session, installs no Zone and performs no operation until a method is called. Independent ports stay independent across await. The existing navigation.push(destination) API remains supported.

Choose placement, not a hidden navigator #

context selects the nearest session. The Graph still selects the destination's Mount and Scope. A call made in a tab can therefore open a route mounted at the root without accidentally placing it in the tab.

For a route mounted more than once, select its exact placement:

final NavigationTarget<bool> target =
    const ProductDestination(productId: 'keyboard-42').placed(
  mountId: const NavigationMountId('catalog-tab'),
);
final NavigationTicket<bool> ticket = target.push(context);

Use an ID actually declared in your Graph. A NavigationTarget<R> has the same five methods and an on(port) binding; it preserves Mount, Scope and presentation. Passing scopeId explicitly filters placements when no Mount is supplied. Supplying both Mount and Scope explicitly can override that Mount's normal Scope; do so only when the application's placement contract intends it.

Ark Navigation never picks the first of several matching Mounts. Ambiguous requests produce NavigationFailed in ticket.committed.

For an existing entry use navigation.pop, popUntil or remove. A destination value is not an entry identity, so it cannot unambiguously choose which duplicate entry to pop. Multi-destination rewrites remain on NavigationPort.

See the route-call guide and stack operations.

Redirect and Guard #

A Redirect selects another typed target before Guards run. A Guard only permits or denies a transition:

final class AuthenticationRedirect implements NavigationRedirect {
  const AuthenticationRedirect(this.session);

  final SessionState session;

  @override
  NavigationTarget<Object?>? redirect(NavigationPolicyContext context) {
    if (session.isSignedIn || context.target.destination is LoginDestination) {
      return null;
    }
    return NavigationTarget<Object?>(
      destination: LoginDestination(returnTo: context.target.destination),
    );
  }
}

final class UnsavedChangesGuard implements NavigationGuard {
  const UnsavedChangesGuard(this.editor);

  final EditorState editor;

  @override
  NavigationGuardDecision evaluate(NavigationPolicyContext context) {
    if (context.phase == NavigationGuardPhase.leave && editor.hasChanges) {
      return const NavigationGuardDenied(reason: 'unsaved_changes');
    }
    return const NavigationGuardAllowed();
  }
}

Policies can be attached to the graph, module, mount, or route. Their dependencies are ordinary constructor parameters; the package does not prescribe a DI container.

Modules and nested navigation #

A feature exports a NavigationModule. The application composition root decides where that module is mounted. If one destination type is reachable through several mounts, the caller must select a NavigationMountId; Ark Navigation rejects ambiguous placement.

Each NavigationScopeDefinition owns an independent Navigator stack. Render a known child stack with NavigationOutlet, or the currently active child branch with NavigationActiveOutlet:

const NavigationActiveOutlet(
  parentScopeId: NavigationScopeId('shell'),
)

Branch activation and reset are explicit operations:

await navigation.activateBranch(const NavigationScopeId('catalog-tab'));
await navigation.resetBranch(const NavigationScopeId('profile-tab'));

Restorable routes require a bidirectional NavigationUriCodec. Query parameters are read and written inside that typed codec rather than passed through the navigation API as a raw map. stackPlanBuilder can reconstruct the parent stack required by a directly opened URI.

Use NavigationRestorationPolicy.sessionOnly only for destinations that contain process-local values and cannot be reconstructed after a restart. Such a route may omit its URI codec.

Graph construction validates every canonical codec by encoding a representative Destination, matching the declared pattern, decoding it, and requiring the same canonical URI on re-encode. Empty route, module, mount, and scope identifiers are rejected before a session starts.

NavigationRuntime.updateGraph is atomic across all owned sessions. It first prepares and validates every migration, then applies all of them; a failure leaves every session on the previous graph. When a migration policy moves an active entry out of a removed Scope, the entry is transferred to the selected surviving Scope rather than left attached to a deleted Navigator.

Absolute platform links can be restricted before they reach the graph:

final NavigationPlatformLinkConfiguration platformLinks =
    NavigationPlatformLinkConfiguration(
  domains: <NavigationLinkDomain>[
    NavigationLinkDomain(
      scheme: 'https',
      host: 'example.com',
      pathPrefixes: const <String>['/app'],
    ),
  ],
);

Documentation #

Inspect a navigation request #

Attach an optional, bounded trace recorder when creating a Session:

final trace = NavigationTraceRecorder(capacity: 100);
final session = NavigationSession(
  id: const NavigationSessionId('main-window'),
  graph: graph,
  eventSinks: <NavigationEventSink>[trace],
);

final steps = trace.steps;
final requestSteps = trace.forRequest(steps.last.requestId);

Each step says whether a request was made, redirected, rejected by a Guard, committed, or failed. Redirect and Guard steps include available route, mount, scope, phase, and type metadata. A commit includes revision and entry count. The buffer evicts oldest steps when full, so an older request may be partial. It does not retain Destination values, URIs, Guard reasons, exception objects, stack traces, or snapshots. Application-defined identifier values may still contain sensitive information; protect or clear the trace accordingly.

AI agent skills #

The package ships two version-matched skills:

  • ark-navigation-route-implementation for typed destinations, operations, Redirect, Guard, URI, results, and restoration;
  • ark-navigation-modular-architecture for graphs, modules, mounts, nested scopes, tabs, sessions, and runtime graph updates.
dart run skills@ get -p ark_navigation

Review installed instructions before use and keep the application's route and access policy in project-owned files.

0
likes
0
points
499
downloads

Documentation

Documentation

Publisher

verified publisherarktelos.dev

Weekly Downloads

Typed modular Flutter navigation with explicit stack operations, deep links, nested scopes, and no code generation.

Homepage
Repository (GitLab)
View/report issues

Topics

#deep-linking #flutter #navigation #routing

License

unknown (license)

Dependencies

flutter

More

Packages that depend on ark_navigation