Skip to content

Commit b5333b8

Browse files
committed
[go_router] Fix ShellRoute chrome dropped from semantics tree by ModalBarrier
Every ModalRoute builds a ModalBarrier wrapped in BlockSemantics, which drops the semantics of siblings painted before it up to the nearest semantics boundary. The nested Navigator built for ShellRoute/StatefulShellRoute branches establishes no such boundary, so the block escapes it and prunes earlier-painted shell chrome (side rail, app bar in Row/Column shells). Wraps shell navigators (not the root navigator) in Semantics(container: true) to contain the block. Squash of the two commits on the upstream PR branch: flutter#12353 Fixes flutter/flutter#135656
1 parent 8032c7b commit b5333b8

3 files changed

Lines changed: 234 additions & 8 deletions

File tree

‎packages/go_router/lib/src/builder.dart‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,25 @@ class _CustomNavigator extends StatefulWidget {
135135
required this.errorBuilder,
136136
required this.errorPageBuilder,
137137
required this.requestFocus,
138+
this.isShellNavigator = false,
138139
});
139140

140141
final GlobalKey<NavigatorState> navigatorKey;
141142
final List<NavigatorObserver> observers;
142143

144+
/// Whether this navigator builds the nested Navigator for a
145+
/// [ShellRoute]/[StatefulShellRoute] branch, as opposed to the root
146+
/// [GoRouter] navigator.
147+
///
148+
/// Shell navigators are wrapped in `Semantics(container: true)` so that
149+
/// each route's [ModalBarrier] (which blocks the semantics of
150+
/// previously-painted siblings up to the nearest semantics boundary)
151+
/// cannot reach past the shell's Navigator and drop shell chrome that
152+
/// paints before it (e.g. a side rail or app bar in a `Row`/`Column`
153+
/// shell). The root navigator has no earlier-painted siblings by
154+
/// construction, so it does not need the same containment.
155+
final bool isShellNavigator;
156+
143157
/// The actual [RouteMatchBase]s to be built.
144158
///
145159
/// This can be different from matches in [matchList] if this widget is used
@@ -298,6 +312,7 @@ class _CustomNavigatorState extends State<_CustomNavigator> {
298312
errorBuilder: widget.errorBuilder,
299313
errorPageBuilder: widget.errorPageBuilder,
300314
requestFocus: widget.requestFocus,
315+
isShellNavigator: true,
301316
),
302317
);
303318
},
@@ -505,18 +520,24 @@ class _CustomNavigatorState extends State<_CustomNavigator> {
505520
_updatePages(context);
506521
}
507522
assert(_pages != null);
523+
final navigator = Navigator(
524+
key: widget.navigatorKey,
525+
requestFocus: widget.requestFocus,
526+
restorationScopeId: widget.navigatorRestorationId,
527+
pages: _pages!,
528+
observers: widget.observers,
529+
onPopPage: _handlePopPage,
530+
);
508531
return GoRouterStateRegistryScope(
509532
registry: _registry,
510533
child: HeroControllerScope(
511534
controller: _controller!,
512-
child: Navigator(
513-
key: widget.navigatorKey,
514-
requestFocus: widget.requestFocus,
515-
restorationScopeId: widget.navigatorRestorationId,
516-
pages: _pages!,
517-
observers: widget.observers,
518-
onPopPage: _handlePopPage,
519-
),
535+
// A Navigator does not establish a semantics boundary, so a route's
536+
// ModalBarrier (wrapped in BlockSemantics) can otherwise drop the
537+
// semantics of shell chrome painted before this navigator (e.g. a
538+
// side rail in a Row-based ShellRoute shell). See
539+
// https://github.com/flutter/flutter/issues/135656.
540+
child: widget.isShellNavigator ? Semantics(container: true, child: navigator) : navigator,
520541
),
521542
);
522543
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `ShellRoute`/`StatefulShellRoute` shell chrome (e.g. a side rail or app bar painted before the routed child) being dropped from the semantics tree by the active route's `ModalBarrier`.
3+
version: patch

‎packages/go_router/test/builder_test.dart‎

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,208 @@ void main() {
14331433
},
14341434
);
14351435
});
1436+
1437+
group('Shell navigator semantics boundary', () {
1438+
testWidgets('Chrome painted before a ShellRoute Navigator stays in the '
1439+
'semantics tree alongside routed content', (WidgetTester tester) async {
1440+
final SemanticsHandle semantics = tester.ensureSemantics();
1441+
1442+
final router = GoRouter(
1443+
initialLocation: '/a',
1444+
routes: <RouteBase>[
1445+
ShellRoute(
1446+
builder: (BuildContext context, GoRouterState state, Widget child) {
1447+
return Row(
1448+
children: <Widget>[
1449+
Semantics(
1450+
container: true,
1451+
label: 'chrome',
1452+
child: const SizedBox(width: 40, height: 40),
1453+
),
1454+
Expanded(child: child),
1455+
],
1456+
);
1457+
},
1458+
routes: <RouteBase>[
1459+
GoRoute(
1460+
path: '/a',
1461+
builder: (BuildContext context, GoRouterState state) {
1462+
return const Text('content-a');
1463+
},
1464+
),
1465+
GoRoute(
1466+
path: '/b',
1467+
builder: (BuildContext context, GoRouterState state) {
1468+
return const Text('content-b');
1469+
},
1470+
),
1471+
],
1472+
),
1473+
],
1474+
);
1475+
addTearDown(router.dispose);
1476+
1477+
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
1478+
await tester.pumpAndSettle();
1479+
1480+
// Without the Semantics(container: true) wrap around the shell's
1481+
// Navigator, the route's ModalBarrier (BlockSemantics) drops the
1482+
// chrome painted before it, because a Navigator does not
1483+
// establish a semantics boundary. See
1484+
// https://github.com/flutter/flutter/issues/135656.
1485+
expect(find.bySemanticsLabel('chrome'), findsOneWidget);
1486+
expect(find.bySemanticsLabel('content-a'), findsOneWidget);
1487+
1488+
// Navigation between two routes within the same shell keeps
1489+
// working, and the chrome remains exposed after the rebuild.
1490+
router.go('/b');
1491+
await tester.pumpAndSettle();
1492+
1493+
expect(find.bySemanticsLabel('chrome'), findsOneWidget);
1494+
expect(find.bySemanticsLabel('content-b'), findsOneWidget);
1495+
expect(find.bySemanticsLabel('content-a'), findsNothing);
1496+
1497+
semantics.dispose();
1498+
});
1499+
1500+
testWidgets('ShellRoute Navigator is wrapped in a Semantics(container: true) '
1501+
'boundary', (WidgetTester tester) async {
1502+
final shellNavigatorKey = GlobalKey<NavigatorState>();
1503+
final router = GoRouter(
1504+
initialLocation: '/',
1505+
routes: <RouteBase>[
1506+
ShellRoute(
1507+
navigatorKey: shellNavigatorKey,
1508+
builder: (BuildContext context, GoRouterState state, Widget child) {
1509+
return child;
1510+
},
1511+
routes: <RouteBase>[
1512+
GoRoute(
1513+
path: '/',
1514+
builder: (BuildContext context, GoRouterState state) {
1515+
return const Text('content');
1516+
},
1517+
),
1518+
],
1519+
),
1520+
],
1521+
);
1522+
addTearDown(router.dispose);
1523+
1524+
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
1525+
await tester.pumpAndSettle();
1526+
1527+
final Iterable<Semantics> ancestorSemantics = tester.widgetList<Semantics>(
1528+
find.ancestor(of: find.byKey(shellNavigatorKey), matching: find.byType(Semantics)),
1529+
);
1530+
expect(
1531+
ancestorSemantics.first.container,
1532+
isTrue,
1533+
reason:
1534+
'The nearest Semantics ancestor of the shell Navigator '
1535+
'should be the container boundary added by go_router.',
1536+
);
1537+
});
1538+
1539+
testWidgets('Root GoRouter Navigator is not wrapped in an extra Semantics '
1540+
'container boundary', (WidgetTester tester) async {
1541+
final rootNavigatorKey = GlobalKey<NavigatorState>();
1542+
final router = GoRouter(
1543+
navigatorKey: rootNavigatorKey,
1544+
initialLocation: '/',
1545+
routes: <RouteBase>[
1546+
GoRoute(
1547+
path: '/',
1548+
builder: (BuildContext context, GoRouterState state) {
1549+
return const Text('content');
1550+
},
1551+
),
1552+
],
1553+
);
1554+
addTearDown(router.dispose);
1555+
1556+
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
1557+
await tester.pumpAndSettle();
1558+
1559+
// The root navigator has no earlier-painted siblings by
1560+
// construction, so go_router must not add the shell-only
1561+
// Semantics(container: true) wrap around it.
1562+
final Finder wrapper = find.ancestor(
1563+
of: find.byKey(rootNavigatorKey),
1564+
matching: find.byWidgetPredicate(
1565+
(Widget widget) => widget is Semantics && widget.container,
1566+
),
1567+
);
1568+
expect(wrapper, findsNothing);
1569+
});
1570+
1571+
testWidgets('Chrome painted before a StatefulShellRoute Navigator stays in the '
1572+
'semantics tree alongside routed content', (WidgetTester tester) async {
1573+
final SemanticsHandle semantics = tester.ensureSemantics();
1574+
StatefulNavigationShell? navigationShell;
1575+
1576+
final router = GoRouter(
1577+
initialLocation: '/a',
1578+
routes: <RouteBase>[
1579+
StatefulShellRoute.indexedStack(
1580+
builder: (BuildContext context, GoRouterState state, StatefulNavigationShell shell) {
1581+
navigationShell = shell;
1582+
return Column(
1583+
children: <Widget>[
1584+
Semantics(container: true, label: 'chrome', child: const SizedBox(height: 10)),
1585+
Expanded(child: shell),
1586+
],
1587+
);
1588+
},
1589+
branches: <StatefulShellBranch>[
1590+
StatefulShellBranch(
1591+
routes: <RouteBase>[
1592+
GoRoute(
1593+
path: '/a',
1594+
builder: (BuildContext context, GoRouterState state) {
1595+
return const Text('content-a');
1596+
},
1597+
),
1598+
],
1599+
),
1600+
StatefulShellBranch(
1601+
routes: <RouteBase>[
1602+
GoRoute(
1603+
path: '/b',
1604+
builder: (BuildContext context, GoRouterState state) {
1605+
return const Text('content-b');
1606+
},
1607+
),
1608+
],
1609+
),
1610+
],
1611+
),
1612+
],
1613+
);
1614+
addTearDown(router.dispose);
1615+
1616+
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
1617+
await tester.pumpAndSettle();
1618+
1619+
// Without the Semantics(container: true) wrap around the shell
1620+
// branch's Navigator, the route's ModalBarrier (BlockSemantics)
1621+
// drops the chrome painted before it, because a Navigator does not
1622+
// establish a semantics boundary. See
1623+
// https://github.com/flutter/flutter/issues/135656.
1624+
expect(find.bySemanticsLabel('chrome'), findsOneWidget);
1625+
expect(find.bySemanticsLabel('content-a'), findsOneWidget);
1626+
1627+
// Switching branches keeps the chrome exposed after the rebuild.
1628+
navigationShell!.goBranch(1);
1629+
await tester.pumpAndSettle();
1630+
1631+
expect(find.bySemanticsLabel('chrome'), findsOneWidget);
1632+
expect(find.bySemanticsLabel('content-b'), findsOneWidget);
1633+
expect(find.bySemanticsLabel('content-a'), findsNothing);
1634+
1635+
semantics.dispose();
1636+
});
1637+
});
14361638
});
14371639
}
14381640

0 commit comments

Comments
 (0)