Learn

Native Back and unsaved changes

Interactive Back, live Guard permission, typed completion and nested stacks.

v2.4.1Intermediateark_navigation

Use this guide when a page should support the platform's Back gesture without losing asynchronous confirmation for unsaved changes. For page-factory setup, start with page design. For general access policies, see Redirect and Guard.

Native Back integration is available starting with 2.2.0.

1. Choose a page that supplies the gesture

Ark Navigation does not implement a swipe recognizer or import a design system. Your application selects a NavigationPageFactory. For example, a CupertinoPage supplies the iOS interactive Back gesture; Android predictive Back requires a compatible page, transition builder and application/platform configuration. A neutral page, fade or slide does not acquire native gestures just because the Controller permits Back.

Keep the supplied Page key, name, restorationId and child. The child contains the router's Back boundary; replacing it also removes that integration.

For a page without Guards, no new policy is needed. Native Back is admitted when that page is the eligible top entry and the Controller is idle. Existing PopScope/Form restrictions inside the page still participate in Flutter's decision.

Use one authoritative leave policy for confirmation. If both a separate Form and this Guard block pop, the router cannot inspect Flutter's private list of PopEntries to distinguish their reasons. The Guard fallback, like an explicit Port operation, is Controller-driven; it is not an additional Form validation pass. Put the combined leave decision in the Guard rather than depending on two independent confirmation mechanisms.

2. Separate immediate permission from confirmation

An interactive gesture needs permission before it starts. An ordinary NavigationGuard.evaluate may show a dialog, perform work or return a Future; the router must not run it speculatively during build.

NavigationBackGuard is an optional extension of NavigationGuard:

dart
final class EditorGuard implements NavigationBackGuard {
  EditorGuard(this.confirmDiscard);

  final Future<bool> Function() confirmDiscard;

  @override
  final ValueNotifier<bool> canPop = ValueNotifier<bool>(true);

  @override
  Future<NavigationGuardDecision> evaluate(
    final NavigationPolicyContext context,
  ) async =>
      context.phase != NavigationGuardPhase.leave ||
          canPop.value ||
          await confirmDiscard()
      ? const NavigationGuardAllowed()
      : const NavigationGuardDenied(reason: EditorReason.unsavedDraft);
}

enum EditorReason { unsavedDraft }

Import ValueNotifier from package:flutter/foundation.dart and the navigation types from package:ark_navigation/ark_navigation.dart.

The application creates this Guard, adds it to the editor's Route Definition through guards: <NavigationGuard>[editorGuard], and updates editorGuard.canPop.value when the draft becomes dirty or saved:

  • true: leaving needs no confirmation. Native pop is authorized without a second call to evaluate after the page has already closed.
  • false: the native gesture is disabled. An explicit Back button using await context.navigation.pop() still calls evaluate and can show the confirmation dialog. A denied decision keeps the entry in place.

The signal is stable and application-owned. Dispose it after its Session/pages stop using the Guard. Do not return a new notifier on every getter access. Use independent Guard state for independent editors; a single global draft flag would couple their permissions.

All effective Guards at Graph, Module, Mount and Route level must implement this contract and currently allow native pop. One legacy asynchronous Guard keeps the native path blocked, even when it normally allows leaving. Existing Guards continue to work unchanged through explicit navigation. For an enter-only Guard, explicitly supply the native permission rather than relying on the router to infer intent from user code.

On iOS, a blocked swipe may never begin and therefore cannot be used to open a confirmation dialog. Provide an accessible Back button. On a blocked system Back attempt delivered to the page, Ark Navigation can use its ordinary Guard path; this is not an interactive gesture waiting for a dialog.

3. What happens during a gesture

An admitted gesture reserves its entry in that Session. Submitted push, replacement, reconciliation, branch changes and Graph updates wait for the gesture to end. They cannot change which entry the gesture closes.

  • Cancel: no Snapshot commit, ticket completion or entry exit; waiting work then continues.
  • Commit: remove the captured entry once and deliver its actual pop result to NavigationTicket<R>.completed as NavigationPopped<R>.
  • Session disposal: release the reservation, remove outstanding entries and reject waiting work instead of leaving the queue blocked.

Permission is fixed for the admitted gesture. A policy change during it governs the next gesture/queued operation, not a late veto of a transition Flutter has already accepted. Applications must not treat UI navigation as their only authorization boundary for sensitive business operations.

4. Nested stacks and dialogs

Back targets the visible Navigator, not merely a retained branch in the Snapshot. A root page or root dialog covering a nested host receives Back before the hidden child stack. A pageless dialog does not replace the page's restorable URI.

Within a nested stack, Back removes the top entry when a previous page exists. At its first page, the attempt bubbles to the parent without clearing the child's retained entries. Closing the host therefore differs from resetting a tab. The final root page bubbles to the platform; exiting the application is not a normal result-bearing pop. Explicit pop(scopeId: ...) retains its separate stack-operation semantics.

5. Avoid bypassing the policy boundary

Use NavigationPort or typed Destination/Target operations for application navigation. Flutter's forced Navigator.pop and removeRoute are not Guarded operations and can bypass canPop.

Ark Navigation reconciles observed mutations by entry ID. A successful pop keeps its result; a removal completes as NavigationRemoved, not a fake successful pop with null. Repeated callbacks do not close the ticket twice. An incompatible result from a forced external pop reports a failure and completes the ticket Future with an error; it does not restore a screen that Flutter has already removed. Handle that Future if external Navigator mutation is unavoidable. Custom Routes must honor Flutter's PopEntry, result-callback and user-gesture lifecycle contracts.

Run and verify the example

The complete editor example includes a Session, Graph, Cupertino factory, live permission, explicit Back and discard dialog. It is application source, not a platform runner. Create a Flutter app, add ark_navigation: ^2.4.1 and cupertino_ui: ^1.1.7, place the example contents in lib/main.dart, then run from that application directory:

console
flutter run

Open the editor while saved, try a partial Back swipe and cancel it, then complete a swipe. Reopen, make an unsaved change, and check that the swipe is disabled. Use Back → Keep editing, then Back → Discard. Saving should enable the native path again. Cupertino gestures depend on the target platform.

Automated coverage uses real Cupertino page widgets and pointer gestures, plus Android predictive-Back platform-channel start/update/cancel/commit events, on Flutter 3.50.1 / Dart 3.14.0. These are widget tests, not physical-device or emulator certification. Before shipping an app, repeat the scenarios on its supported iOS/Android versions, including its own Forms, dialogs, page factory, nested hosts and system navigation settings.

Two additional integration scenarios passed on an iPhone 17 Pro / iOS 26.5 Simulator: confirmation/save, swipe cancellation/commit and dirty-editor veto. This does not certify physical devices or Android system predictive Back.