Skip to content

Commit 511d2e3

Browse files
veto9292claude
andcommitted
fix(router): keep the back button working after a settings sub-page
The PopScope that sent back to home lived inside the settings branch. Branches stay mounted after the user leaves them, so it kept answering presses meant for other branches and the app could no longer be closed with back at all. Reported upstream as flutter/flutter#193098. Move the handling to the shell, where exactly one instance exists and it always belongs to the branch on screen. The NotificationListener beside it keeps the platform told that Flutter handles back; without it Android stops believing that once a branch pops a page, and closes the app without calling Flutter. Verified on an Android 16 emulator with both the back button and the edge swipe. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 502f1ee commit 511d2e3

2 files changed

Lines changed: 74 additions & 41 deletions

File tree

‎lib/core/router/adaptive_layout/my_adaptive_layout.dart‎

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,82 @@ class MyAdaptiveLayout extends HookConsumerWidget {
5353
HardwareKeyboard.instance.removeHandler(handler);
5454
};
5555
}, [isMobileBreakpoint, showProfilesAction, navigationShell.currentIndex]);
56-
return Material(
57-
child: Scaffold(
58-
body: isMobileBreakpoint
59-
? navigationShell
60-
: Row(
61-
children: [
62-
FocusScope(
56+
// Owns the system back press for the whole shell.
57+
//
58+
// It has to sit here, on the visible shell, and nowhere else. A PopScope
59+
// inside a branch stays mounted after the user leaves that branch, and go
60+
// router then lets it answer presses meant for another branch, which locks
61+
// the back button for good (flutter/flutter#193098).
62+
//
63+
// canPop is always false so the platform always routes the press to us.
64+
// Otherwise Android closes the app without calling into Flutter at all, and
65+
// no callback of ours ever runs. A press that a branch can handle is popped
66+
// by go router before it reaches here, so this only sees presses at the
67+
// root of a branch.
68+
return PopScope(
69+
canPop: false,
70+
onPopInvokedWithResult: (didPop, _) {
71+
if (didPop) return;
72+
final homeIndex = getIndexOfBranch(isMobileBreakpoint, showProfilesAction, 'home');
73+
if (navigationShell.currentIndex != homeIndex) {
74+
// on another branch: go home instead of closing the app
75+
navigationShell.goBranch(homeIndex);
76+
} else {
77+
// already home: this is the way out of the app
78+
SystemNavigator.pop();
79+
}
80+
},
81+
// Keeps the claim above alive on the platform side.
82+
//
83+
// Every branch navigator broadcasts whether it can handle a back press,
84+
// and the last broadcast wins. Popping a page inside a branch leaves it
85+
// reporting "cannot handle", which reaches Android and makes it close the
86+
// app on the next press without ever calling Flutter. The PopScope above
87+
// always handles the press, so that report is wrong: replace it.
88+
child: NotificationListener<NavigationNotification>(
89+
onNotification: (notification) {
90+
if (notification.canHandlePop) return false;
91+
const NavigationNotification(canHandlePop: true).dispatch(context);
92+
return true;
93+
},
94+
child: Material(
95+
child: Scaffold(
96+
body: isMobileBreakpoint
97+
? navigationShell
98+
: Row(
99+
children: [
100+
FocusScope(
101+
node: navScopeNode,
102+
child: NavigationRail(
103+
extended: Breakpoint(context).isDesktop(),
104+
destinations: _navRailDests(_actions(t, showProfilesAction, isMobileBreakpoint)),
105+
selectedIndex: navigationShell.currentIndex,
106+
onDestinationSelected: (index) => _onTap(context, index),
107+
trailing: Breakpoint(context).isDesktop()
108+
? const Expanded(
109+
child: Align(
110+
alignment: Alignment.bottomCenter,
111+
child: SizedBox(width: 220, child: SideBarStatsOverview()),
112+
),
113+
)
114+
: null,
115+
),
116+
),
117+
Expanded(child: navigationShell),
118+
],
119+
),
120+
bottomNavigationBar: isMobileBreakpoint
121+
? FocusScope(
63122
node: navScopeNode,
64-
child: NavigationRail(
65-
extended: Breakpoint(context).isDesktop(),
66-
destinations: _navRailDests(_actions(t, showProfilesAction, isMobileBreakpoint)),
67-
selectedIndex: navigationShell.currentIndex,
123+
child: NavigationBar(
124+
selectedIndex: navigationShell.currentIndex <= 1 ? navigationShell.currentIndex : 0,
125+
destinations: _navDests(_actions(t, showProfilesAction, isMobileBreakpoint)),
68126
onDestinationSelected: (index) => _onTap(context, index),
69-
trailing: Breakpoint(context).isDesktop()
70-
? const Expanded(
71-
child: Align(
72-
alignment: Alignment.bottomCenter,
73-
child: SizedBox(width: 220, child: SideBarStatsOverview()),
74-
),
75-
)
76-
: null,
77127
),
78-
),
79-
Expanded(child: navigationShell),
80-
],
81-
),
82-
bottomNavigationBar: isMobileBreakpoint
83-
? FocusScope(
84-
node: navScopeNode,
85-
child: NavigationBar(
86-
selectedIndex: navigationShell.currentIndex <= 1 ? navigationShell.currentIndex : 0,
87-
destinations: _navDests(_actions(t, showProfilesAction, isMobileBreakpoint)),
88-
onDestinationSelected: (index) => _onTap(context, index),
89-
),
90-
)
91-
: null,
128+
)
129+
: null,
130+
),
131+
),
92132
),
93133
);
94134
}

‎lib/core/router/go_router/routing_config_notifier.dart‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,7 @@ class RoutingConfigNotifier extends _$RoutingConfigNotifier {
173173
GoRoute(
174174
name: 'settings',
175175
path: '/settings',
176-
builder: (context, _) => FocusScope(
177-
node: branchesScope['settings'],
178-
child: PopScope(
179-
canPop: false,
180-
onPopInvokedWithResult: (_, _) => context.goNamed('home'),
181-
child: SettingsPage(),
182-
),
183-
),
176+
builder: (_, _) => FocusScope(node: branchesScope['settings'], child: SettingsPage()),
184177
routes: <GoRoute>[
185178
GoRoute(
186179
name: 'general',

0 commit comments

Comments
 (0)