Tutorials

Move retained tabs into feature Modules

Mount one Module in guest and private areas, retain tabs and restore application Shells from a direct link.

v2.4.1Intermediate

The complete Widget-test example connects the same Catalog Module to two applications. Each application has a guest catalog and a private account containing Catalog/Profile tabs. It uses the current runtime, without another routing package or a new adapter API.

Available since 2.4.0: the example now compares cold restoration with and without NavigationMount.uriParentPlan. With the option, the host supplies Portal (guest) or Portal → Account (private); the unchanged Module supplies Catalog → Product. Earlier versions do not have this option.

1. Run and read the checks

From the package checkout:

shell
flutter test example/migration/modular_hosts_migration_test.dart

There are fourteen checks: six ordinary navigation cases, four opt-in cold-link cases and four null-option controls. The applications run sequentially in Widget tests, not as competing root Routers in one Flutter view.

Start with _CatalogFeature, then _HostAppState._graph, then the first test. _Host holds application-specific prefixes; _S groups the example's typed host identities. The feature does not read either of them.

2. Decide which owner holds each stack

ScopeParentPages / responsibility
rootnoneThe application's Portal Shell and its top-level controls
guestrootPublic Catalog/Product; remains after logout
accountrootAccount Shell with the two private tab controls
account-catalogaccountThe private Catalog/Product stack
account-profileaccountThe private Profile stack

Portal renders the active guest/account branch. Account Shell renders the active Catalog/Profile branch through NavigationActiveOutlet. A Scope's parent relationship does not automatically create a page containing that Outlet.

When migrating a tab router, write down these owners first. Do not flatten the old branch stacks into one stack or equate a selected tab index with ownership.

3. Keep Catalog independent of the application

_CatalogFeature.module is one immutable Module instance containing Catalog and Product. Its routes know only relative /, /products/:id, the old alias /item/:id, their page builders and a feature-local Catalog → Product plan.

The host creates the Scopes, Shell pages, Mounts, access policy and Session. The example uses these prefixes:

Placementconsumer applicationstaff application
Guest Catalog/browse/shop
Account Shell/account/workspace
Private Catalog/account/catalog/workspace/inventory
Profile/account/profile/workspace/profile

The reuse test passes the exact same Module object to both Graphs and checks identity with same(shared). It also opens actual Product widgets and checks the different resulting URIs. This is source/configuration reuse, not an assertion that the two applications share their navigation state.

4. Choose the intended Mount at the call

Catalog is mounted twice in each Graph. The context selects the Session, not which Catalog placement the caller intended. Product opening inside Catalog is:

dart
final NavigationTicket<String> ticket = _Product(
  'sku-42',
).placed(mountId: widget.mount).push(context);

The Catalog page receives its current Entry's mountId from its page builder; there is no hardcoded guest/private identifier in the feature. A host-owned Profile action explicitly chooses the private Mount. Its Product opens in the Catalog stack, while the Profile Entry remains unchanged.

A call without Mount is tested separately: it fails without changing the Snapshot and completes as NavigationNotCommitted. The example does not resolve ambiguity by route order or by whichever Widget happened to call push.

5. Preserve tabs; end the private lifecycle explicitly

The main scenario first opens a guest Product, then attempts Account while signed out. The Guard denial preserves the same Snapshot. After the local fake access state permits entry, the host initializes Account, Catalog and Profile and activates Catalog. These are separate commits, not an atomic multi-Scope initialization API.

Selecting a Product returns the typed ID to its still-mounted caller. A second Product stays in Catalog while Profile is shown; switching back must reveal that same Entry ID. This proves retained logical navigation and the rendered page, not retention of every local State object in an inactive subtree.

The relevant part of the host's logout is:

dart
access.signedIn = false;
if (!await _accept(session.controller.resetBranch(_S.account))) return;
await guest();

resetBranch removes Account and its descendants. The guest Product keeps its original Entry ID and becomes visible again. The observer checks one removed notification for the private Product and no removed notification for the guest. Final Session disposal produces the guest's sessionClosed notification.

The fake boolean is not an authentication implementation. Server logout, token revocation and application resource cleanup remain application responsibilities. A lifecycle callback alone does not acknowledge asynchronous resource closure.

6. Do not mistake a decoded URL for a visible page

Eight checks open the alias under both guest/private placements in both hosts. Four keep the option null: local Catalog/Product entries exist but root has no Portal, so Product is not rendered. Four enable uriParents: the host Mount supplies a fixed list through NavigationUriParentPlan.fixed, and Product is actually visible. The private plan also restores Account. No controller seeding or host identifiers inside the feature Module are needed.

The complete configuration is in the linked source: inspect _HostApp.uriParents and the guest/private Mounts in _HostAppState._graph. Set the flag to true when opening this example directly by URI. For an existing application, declare the same parents on its Mount; the flag is only a comparison switch in the example.

For parameter-dependent parents, implement NavigationUriParentPlan.buildParents. It receives the resolved leaf, including Destination and Mount; use Dart pattern matching for the expected Destination type. Return the full prefix, not a recursive link to another builder. Parents must resolve along the root-to-target Scope path in order and have restorable URI codecs. Same-Scope Home → Product is allowed; intermediate logical Scopes need not own pages.

The new restore pipeline settles Redirects for the whole plan before Guards. A parent redirected to Login discards the private Catalog/Product tail. A guard refusal leaves the previous Snapshot unchanged. Ordinary push does not invoke this builder. Valid saved history preserves the actual stacks without either plan builder; it may contain a different Shell from the cold-link default. A structurally damaged state permits one full URI fallback, not partial replay or retry after a Guard refusal. These Controller/Widget contracts do not prove actual browser Back/Forward behavior.

7. Transfer a real application's tab configuration

  1. Inventory branch owners, initial pages, retained identities and logout scope.
  2. Move feature Destinations/routes/relative codecs into its Module.
  3. Declare the host's Scopes, Shell pages, Mounts and access rules separately.
  4. Replace an intentional tab selection with activateBranch, not a new push.
  5. Check real Widget output together with exact stack identities and exits.
  6. Treat cold URLs and browser history as separate acceptance cases. Do not release an application needing them while its parent plan remains incomplete.

The example does not execute go_router/auto_route, OS link delivery, interactive gestures or browser Back/Forward. See the basic migration guide for the source-router comparison and a complete before/after Navigator case.

For a separate runnable Web host and the real browser journey, follow URI parent plans in Flutter Web.