Tutorials
Check host URI parents in a real browser
Run a Widgets-only application and check aliases, Back, Forward, the actual nested stack and refresh.
The host parent-plan API is available since 2.4.0. The complete Widgets-only host has a Portal in the root Navigator and a feature Catalog/Product stack in a child Navigator. The feature owns its relative codecs and Catalog → Product plan; its Mount owns the Portal prefix. Material, Cupertino and a second router are not required.
Run from the repository checkout
flutter run -d web-server --web-hostname ::1 --web-port 18743 --target example/uri_parent_plan_web.dart
Open http://[::1]:18743/#/catalog/item/42. The example uses Flutter's default
hash URL strategy. For a static release build, use:
flutter build web --target example/uri_parent_plan_web.dart --no-pub
python3 -m http.server 18743 --bind ::1 --directory build/web
The host explicitly passes the platform's initial route to NavigationSession.
Without an explicit initialUri, Session's existing default is /; this example
does not silently change that default. It keeps semantics enabled and gives
the child Outlet its own semantics container so its route does not hide the
header from screen readers.
An exact NavigationOutlet subscribes to Controller changes itself. A const Outlet inside an unchanged Shell still updates when its child stack changes; application code does not need a surrounding ListenableBuilder to repair it.
Check the actual journey
- Alias
/catalog/item/42: Portal and Product 42 are visible; the URL becomes#/catalog/products/42without adding a separate alias history entry. - Push product 99: Product 99 is visible and the URL changes accordingly.
- Browser Back returns to Product 42; Forward returns to Product 99.
- Pop child now returns to Product 42. This distinguishes replay of the actual saved stack from a cold plan containing only Catalog → Product 99.
- Refresh at Product 42 reconstructs Portal and the local feature plan. Pop child then reaches Catalog, not an empty root.
- Open canonical
#/catalog/products/99directly: Portal and Product 99 appear.
The browser driver repeats these checks
with an already installed Playwright module. Set ARK_NAVIGATION_PLAYWRIGHT to
the absolute path of that module, then run node tool/uri_parent_plan_web_check.cjs
while the static build is served. It does not install packages. Set
ARK_NAVIGATION_WEB_ORIGIN to http://[::1]:18743 for this IPv6 loopback host. A failed run writes a
screenshot into build; successful runs report each completed check and page errors.
The successful final page is also captured in build/uri-parent-plan-web.png.
This is desktop Chromium acceptance of hash URLs, not a claim about Safari, mobile OS links, path-strategy server rewrites, or native physical gestures. Private access and retained inactive branches are checked separately in Controller and modular Widget tests; this browser host does not simulate auth.
Complete runnable host
This is the complete example file, not an isolated configuration fragment. The commands above run this file, including all Destinations, Scopes and Mounts.
import 'dart:async';
import 'package:ark_navigation/ark_navigation.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter/widgets.dart';
void main() => runApp(const UriParentPlanWebExample());
/// Runnable Widgets-only host for direct links and real browser history.
final class UriParentPlanWebExample extends StatefulWidget {
const UriParentPlanWebExample({super.key});
@override
State<UriParentPlanWebExample> createState() => _ExampleState();
}
final class _ExampleState extends State<UriParentPlanWebExample> {
// Keep this QA/example surface accessible to keyboard and browser automation.
late final SemanticsHandle _semantics;
@override
void initState() {
super.initState();
_semantics = WidgetsBinding.instance.ensureSemantics();
}
late final NavigationSession _session = NavigationSession(
id: const NavigationSessionId('uri-parents-web'),
graph: _graph(),
// Explicit host ownership of the initial platform URL, rather than '/'.
initialUri: Uri.parse(
WidgetsBinding.instance.platformDispatcher.defaultRouteName,
),
);
NavigationGraph _graph() => NavigationGraph(
rootScopeId: _Ids.root,
scopes: const <NavigationScopeDefinition>[
NavigationScopeDefinition(id: _Ids.root),
NavigationScopeDefinition(id: _Ids.catalog, parentId: _Ids.root),
],
modules: <NavigationModule>[
NavigationModule(
id: _Ids.hostModule,
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Portal, void>(
id: const NavigationRouteId('portal'),
pageBuilder:
(
final BuildContext context,
final _Portal destination,
final NavigationEntrySnapshot entry,
) => ColoredBox(
color: const Color(0xfff6f3ee),
child: SafeArea(
child: Column(
children: <Widget>[
const Text(
'Portal shell',
style: TextStyle(
fontSize: 24,
color: Color(0xff182538),
),
),
_Action(
'Open catalog',
() => unawaited(
_session.controller.restoreUri(
Uri.parse('/catalog'),
historyMode: NavigationHistoryMode.push,
),
),
),
ListenableBuilder(
listenable: _session.controller,
builder:
(
final BuildContext context,
final Widget? child,
) => Text(
'URI: ${_session.controller.currentUri}',
),
),
Expanded(
child: Semantics(
container: true,
explicitChildNodes: true,
child: const NavigationOutlet(
scopeId: _Ids.catalog,
),
),
),
],
),
),
),
uriCodec: CallbackNavigationUriCodec<_Portal>(
pattern: '/',
encoder: (final _Portal destination) => Uri(path: '/'),
decoder: (final NavigationUriMatch match) => const _Portal(),
),
),
],
),
NavigationModule(
id: _Ids.featureModule,
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Catalog, void>(
id: const NavigationRouteId('catalog'),
pageBuilder:
(
final BuildContext context,
final _Catalog destination,
final NavigationEntrySnapshot entry,
) => _page('Catalog'),
uriCodec: CallbackNavigationUriCodec<_Catalog>(
pattern: '/',
encoder: (final _Catalog destination) => Uri(path: '/'),
decoder: (final NavigationUriMatch match) => const _Catalog(),
),
),
NavigationRouteDefinition<_Product, void>(
id: const NavigationRouteId('product'),
pageBuilder:
(
final BuildContext context,
final _Product destination,
final NavigationEntrySnapshot entry,
) => _page('Product ${destination.id}'),
uriCodec: CallbackNavigationUriCodec<_Product>(
pattern: '/products/:id',
encoder: (final _Product destination) =>
Uri(path: '/products/${destination.id}'),
decoder: (final NavigationUriMatch match) =>
_Product(match.path('id')),
),
uriAliases: <NavigationUriAlias<_Product>>[
CallbackNavigationUriAlias<_Product>(
pattern: '/item/:id',
decoder: (final NavigationUriMatch match) =>
_Product(match.path('id')),
),
],
stackPlanBuilder: (final _Product destination) =>
<NavigationTarget<Object?>>[
const NavigationTarget<void>(destination: _Catalog()),
NavigationTarget<void>(destination: destination),
],
),
],
),
],
mounts: <NavigationMount>[
NavigationMount(
id: _Ids.hostMount,
moduleId: _Ids.hostModule,
scopeId: _Ids.root,
),
NavigationMount(
id: _Ids.featureMount,
moduleId: _Ids.featureModule,
scopeId: _Ids.catalog,
pathPrefix: '/catalog',
uriParentPlan: NavigationUriParentPlan.fixed(
const <NavigationTarget<Object?>>[
NavigationTarget<void>(
destination: _Portal(),
mountId: _Ids.hostMount,
),
],
),
),
],
);
Widget _page(final String title) => ColoredBox(
color: const Color(0xffffffff),
child: Column(
children: <Widget>[
Text(
title,
style: const TextStyle(fontSize: 32, color: Color(0xff182538)),
),
for (final String id in <String>['42', '99'])
_Action('Push product $id', () {
unawaited(
_session.controller
.pushTarget(
NavigationTarget<void>(
destination: _Product(id),
mountId: _Ids.featureMount,
),
)
.committed,
);
}),
_Action('Pop child', () {
unawaited(_session.controller.pop(scopeId: _Ids.catalog));
}),
],
),
);
@override
Widget build(final BuildContext context) => WidgetsApp.router(
color: const Color(0xff182538),
textStyle: const TextStyle(fontSize: 18, color: Color(0xff182538)),
routerConfig: _session.routerConfig,
);
@override
void dispose() {
_session.dispose();
_semantics.dispose();
super.dispose();
}
}
final class _Action extends StatelessWidget {
const _Action(this.label, this.onTap);
final String label;
final VoidCallback onTap;
@override
Widget build(final BuildContext context) => Semantics(
button: true,
label: label,
excludeSemantics: true,
onTap: onTap,
child: GestureDetector(
onTap: onTap,
excludeFromSemantics: true,
child: Padding(
padding: const EdgeInsets.all(14),
child: Text(label, style: const TextStyle(color: Color(0xff235db6))),
),
),
);
}
abstract final class _Ids {
static const NavigationScopeId root = NavigationScopeId('root');
static const NavigationScopeId catalog = NavigationScopeId('catalog');
static const NavigationModuleId hostModule = NavigationModuleId('host');
static const NavigationModuleId featureModule = NavigationModuleId('feature');
static const NavigationMountId hostMount = NavigationMountId('host');
static const NavigationMountId featureMount = NavigationMountId('catalog');
}
final class _Portal extends NavigationDestination<void> {
const _Portal();
}
final class _Catalog extends NavigationDestination<void> {
const _Catalog();
}
final class _Product extends NavigationDestination<void> {
const _Product(this.id);
final String id;
}