Require double back to exit Android tab roots

This commit is contained in:
2026-10-24 02:23:15 +01:00
parent 54f74ade33
commit 4e6771aa24
6 changed files with 233 additions and 11 deletions
+30 -3
View File
@@ -1,6 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'android_root_exit.dart';
import 'app_route.dart';
/// Parses the browser (or platform) location into the app's routes.
@@ -83,6 +84,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
final bool cupertino;
final _navigatorKey = GlobalKey<NavigatorState>();
late final _rootExit = AndroidRootExit(notifyListeners);
static const _pageKey = ValueKey<String>('pipelines-shell');
static const _searchPageKey = ValueKey<String>('app-search');
static const _templatesPageKey = ValueKey<String>('app-templates');
@@ -142,6 +144,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
: _route;
return Navigator(
key: _navigatorKey,
observers: [_rootExit],
pages: [
// While an item is open inside its tab (a wide screen keeps it
// there), Android's Back and back gesture step up a level instead of
@@ -149,9 +152,11 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
_page(
_pageKey,
PopScope(
canPop: !_canStepUp,
canPop: !_canStepUp && !(_isExitRoot && !_rootExit.armed),
onPopInvokedWithResult: (didPop, _) {
if (!didPop) _stepUp();
if (!didPop && !_stepUp() && _isExitRoot) {
_rootExit.handleBack();
}
},
child: builder(context, shellRoute),
),
@@ -214,19 +219,22 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
@override
Future<void> setInitialRoutePath(AppRoute configuration) async {
_rootExit.clear();
_route = configuration;
}
@override
Future<void> setNewRoutePath(AppRoute configuration) async {
if (configuration == _route) return;
_rootExit.clear();
_route = configuration;
notifyListeners();
}
/// Back (Android's button, the browser's): close whatever page sits on top
/// first, then step up one level — a stage to its run or pipeline, a run,
/// pipeline or chat to its list — and only leave the app from a list.
/// pipeline or chat to its list. Android list roots require two presses;
/// all other platforms retain their normal Back behavior.
@override
Future<bool> popRoute() async {
final navigator = _navigatorKey.currentState;
@@ -244,9 +252,21 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
return true;
}
if (_stepUp()) return true;
if (_isExitRoot) return _rootExit.handleBack();
return false;
}
bool get _isExitRoot =>
_rootExit.enabled &&
!_canStepUp &&
switch (_route.kind) {
AppRouteKind.pipelines ||
AppRouteKind.runs ||
AppRouteKind.chat ||
AppRouteKind.settings => true,
_ => false,
};
/// Whether the open route has a level above it in its own tab (a stage,
/// a run, a pipeline, a chat).
bool get _canStepUp =>
@@ -285,9 +305,16 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
/// history entry (or a replacement when it already matches the location).
void goTo(AppRoute route) {
if (route == _route) return;
_rootExit.clear();
_route = route;
notifyListeners();
}
@override
void dispose() {
_rootExit.dispose();
super.dispose();
}
}
/// The location a launch starts at: the browser location on the web, the