Shell: system Back returns to the previous tab
At a tab's root the system Back now returns to the tab opened before it, through a most-recent-first stack of visited tabs without duplicates. Every tab change (tap, Ctrl+Tab, a routed open) records it. Pages over the shell, Settings and a page in the open item's pane still close first; with no history Back reaches the platform as before. Only the shown tab's detail pane answers Back now.
This commit is contained in:
@@ -12,6 +12,7 @@ import 'list_detail.dart';
|
||||
import 'list_pane_widths.dart';
|
||||
import 'shell_bar.dart';
|
||||
import 'shell_list_shortcuts.dart';
|
||||
import 'tab_history.dart';
|
||||
|
||||
class AdaptiveShell extends StatefulWidget {
|
||||
const AdaptiveShell({
|
||||
@@ -51,6 +52,10 @@ class AdaptiveShell extends StatefulWidget {
|
||||
class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
late AppDestination _selected;
|
||||
final Set<AppDestination> _visited = {};
|
||||
final TabHistory _history = TabHistory();
|
||||
// Whether a navigator inside each destination (an open item's pane) has a
|
||||
// page of its own for the system Back to close first.
|
||||
final Map<AppDestination, bool> _nestedCanPop = {};
|
||||
final ShellBarRegistry _bar = ShellBarRegistry();
|
||||
late final ListPaneWidths _paneWidths = ListPaneWidths(
|
||||
store: widget.listPaneWidthStore,
|
||||
@@ -79,6 +84,7 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final destination = widget.destination;
|
||||
if (destination != null) {
|
||||
_history.record(_selected, destination);
|
||||
_selected = destination;
|
||||
_visited.add(destination);
|
||||
}
|
||||
@@ -91,6 +97,7 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_history.record(_selected, destination);
|
||||
_selected = destination;
|
||||
_visited.add(destination);
|
||||
});
|
||||
@@ -139,13 +146,19 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
final item = destinationDetails(destination);
|
||||
return KeyedSubtree(
|
||||
key: ValueKey(destination),
|
||||
child: ShellBarScope(
|
||||
registry: _bar,
|
||||
destination: destination,
|
||||
child: _visited.contains(destination)
|
||||
? widget.destinationBodies[destination]?.call(context) ??
|
||||
Center(child: Text('${item.label} — Not implemented'))
|
||||
: const SizedBox.shrink(),
|
||||
child: NotificationListener<NavigationNotification>(
|
||||
onNotification: (notification) {
|
||||
_nestedCanPop[destination] = notification.canHandlePop;
|
||||
return false;
|
||||
},
|
||||
child: ShellBarScope(
|
||||
registry: _bar,
|
||||
destination: destination,
|
||||
child: _visited.contains(destination)
|
||||
? widget.destinationBodies[destination]?.call(context) ??
|
||||
Center(child: Text('${item.label} — Not implemented'))
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
@@ -198,12 +211,16 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
// The scaffolds below swap the system insets for their bars'
|
||||
// heights; panes beside a list read the real ones from here.
|
||||
final system = MediaQuery.paddingOf(context);
|
||||
// A page (Settings) closes to Pipelines on the system Back too. The
|
||||
// scope is always there, so opening the page never remounts the shell.
|
||||
// A page (Settings) closes to Pipelines on the system Back too; at a
|
||||
// tab's root Back returns to the tab opened before it. Only with neither
|
||||
// does Back reach the platform, so Android's (predictive) Back closes the
|
||||
// app only from the first tab. Pages pushed over the shell (an item,
|
||||
// Search, a dialog) pop before this scope is asked. The scope is always
|
||||
// there, so opening the page never remounts the shell.
|
||||
Widget backable(Widget child) => PopScope(
|
||||
canPop: !page,
|
||||
canPop: !page && _history.isEmpty,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop) _closePage();
|
||||
if (!didPop) _back(page: page);
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
@@ -242,6 +259,18 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
/// Leaves a page destination (Settings) for the tab it belongs to.
|
||||
void _closePage() => _select(shellTabFor(_selected));
|
||||
|
||||
/// The system Back the shell handles: it closes a page, or else returns to
|
||||
/// the previous tab unless the open item's own pane closes a page first.
|
||||
void _back({required bool page}) {
|
||||
if (page) {
|
||||
_closePage();
|
||||
return;
|
||||
}
|
||||
if (_nestedCanPop[shellTabFor(_selected)] ?? false) return;
|
||||
final previous = _history.takePrevious();
|
||||
if (previous != null) _select(previous);
|
||||
}
|
||||
|
||||
/// Handles this before focused text controls can insert or traverse on
|
||||
/// Ctrl+Tab. The handler is scoped to the shell's lifetime, not onboarding.
|
||||
///
|
||||
|
||||
@@ -12,7 +12,8 @@ import '../ui/adaptive/host.dart';
|
||||
///
|
||||
/// The item is this navigator's first page under a fixed key, so a rebuilt
|
||||
/// item updates in place and is never remounted. The system Back pops a page
|
||||
/// pushed here before it leaves the item. Opening a different item
|
||||
/// pushed here before it leaves the item (or the shell's previous tab), in
|
||||
/// the shown tab only. Opening a different item
|
||||
/// ([identity] changes) closes any page the previous item had pushed, so the
|
||||
/// pane shows the newly selected item.
|
||||
class DetailPaneNavigator extends StatefulWidget {
|
||||
@@ -44,8 +45,15 @@ class _DetailPaneNavigatorState extends State<DetailPaneNavigator> {
|
||||
final Page<void> root = usesCupertinoHost(context)
|
||||
? CupertinoPage<void>(key: _rootKey, child: widget.child)
|
||||
: MaterialPage<void>(key: _rootKey, child: widget.child);
|
||||
// Only the shown tab's pane answers the system Back: a page left open in
|
||||
// another tab's pane stays there while Back acts on the current tab. The
|
||||
// route asks every handler on it, so a hidden pane also ignores the call.
|
||||
final shown = TickerMode.valuesOf(context).enabled;
|
||||
return NavigatorPopHandler(
|
||||
onPopWithResult: (_) => _navigator.currentState?.maybePop(),
|
||||
enabled: shown,
|
||||
onPopWithResult: (_) {
|
||||
if (shown) _navigator.currentState?.maybePop();
|
||||
},
|
||||
child: Navigator(
|
||||
key: _navigator,
|
||||
pages: [root],
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'destination.dart';
|
||||
|
||||
/// The tabs the operator left, most recent first, for the system Back.
|
||||
///
|
||||
/// Every tab change (a tap, Ctrl+Tab, a search result or notification opening
|
||||
/// another tab) goes through [record]: the tab being left moves to the top
|
||||
/// and the tab being opened leaves the stack, so each tab appears at most once
|
||||
/// and the current tab is never in it. A page that belongs to a tab
|
||||
/// (Settings, of Pipelines) is that tab here.
|
||||
///
|
||||
/// [takePrevious] steps back: it removes the top tab and returns it, and the
|
||||
/// next [record] that opens that tab does not push the tab being left, so Back
|
||||
/// never builds a forward trail.
|
||||
class TabHistory {
|
||||
final List<AppDestination> _tabs = [];
|
||||
AppDestination? _returningTo;
|
||||
|
||||
/// The left tabs, most recent first.
|
||||
List<AppDestination> get tabs => List.unmodifiable(_tabs);
|
||||
|
||||
bool get isEmpty => _tabs.isEmpty;
|
||||
|
||||
/// Notes a change of the shown destination from [from] to [to].
|
||||
void record(AppDestination from, AppDestination to) {
|
||||
final left = shellTabFor(from);
|
||||
final opened = shellTabFor(to);
|
||||
if (left == opened) return;
|
||||
final returning = _returningTo == opened;
|
||||
_returningTo = null;
|
||||
_tabs.remove(opened);
|
||||
if (returning) return;
|
||||
_tabs
|
||||
..remove(left)
|
||||
..insert(0, left);
|
||||
}
|
||||
|
||||
/// The tab Back returns to, removed from the stack; null when empty.
|
||||
AppDestination? takePrevious() {
|
||||
if (_tabs.isEmpty) return null;
|
||||
final previous = _tabs.removeAt(0);
|
||||
_returningTo = previous;
|
||||
return previous;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user