Skip to content

Commit c9b172d

Browse files
Philclaude
andcommitted
test: canary for a GlobalKey in a Cupertino bar on a push (flutter/flutter#113409)
A CupertinoNavigationBar whose trailing widget has a GlobalKey throws 'Multiple widgets used the same GlobalKey' when a page with a bar of its own is pushed: the bars' transition between routes builds a copy of the bar while the original stays. It reproduces with auto_route's Cupertino routes as in plain Flutter. Flutter documents the limit under transitionBetweenRoutes, which an app turns off on such a bar; auto_route only picks the route type. The canary pins it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3ce8eed commit c9b172d

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'dart:async';
2+
3+
import 'package:auto_route/auto_route.dart';
4+
import 'package:cupertino_ui/cupertino_ui.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
import 'router_test_utils.dart';
8+
9+
// Canary for Flutter bug https://github.com/flutter/flutter/issues/113409.
10+
// Setup: a router whose pages are Cupertino routes. HomeRoute's
11+
// CupertinoNavigationBar has a trailing widget with a GlobalKey; SecondRoute
12+
// has a CupertinoNavigationBar too. Plain Flutter fails the same way.
13+
// Steps: push SecondRoute.
14+
// Today the bars' transition between routes builds a copy of HomeRoute's bar
15+
// over both pages while the original stays in place, so the keyed widget is
16+
// in the tree twice and Flutter throws "Multiple widgets used the same
17+
// GlobalKey". Flutter documents this under
18+
// CupertinoNavigationBar.transitionBetweenRoutes; an app sets it to false on
19+
// a bar that holds a GlobalKey. auto_route only picks the route type.
20+
// If this test fails after a Flutter upgrade, the transition no longer copies
21+
// keyed widgets: drop this canary.
22+
void main() {
23+
testWidgets('pushing past a Cupertino bar that holds a GlobalKey throws on the duplicate key', (tester) async {
24+
final settingsKey = GlobalKey();
25+
final router = RootStackRouter.build(
26+
defaultRouteType: const .cupertino(),
27+
routes: [
28+
AutoRoute(
29+
path: '/',
30+
page: PageInfo(
31+
'HomeRoute',
32+
builder: (_) => CupertinoPageScaffold(
33+
navigationBar: CupertinoNavigationBar(
34+
middle: const Text('HomeRoute'),
35+
trailing: Icon(CupertinoIcons.settings, key: settingsKey),
36+
),
37+
child: const SizedBox(),
38+
),
39+
),
40+
),
41+
AutoRoute(
42+
path: '/second',
43+
page: PageInfo(
44+
'SecondRoute',
45+
builder: (_) => const CupertinoPageScaffold(
46+
navigationBar: CupertinoNavigationBar(middle: Text('SecondRoute')),
47+
child: SizedBox(),
48+
),
49+
),
50+
),
51+
],
52+
);
53+
await pumpRouterApp(tester, router);
54+
final errors = <String>[];
55+
final onError = FlutterError.onError;
56+
FlutterError.onError = (details) => errors.add(details.exceptionAsString());
57+
unawaited(router.push(const NamedRoute('SecondRoute')));
58+
await tester.pumpAndSettle();
59+
FlutterError.onError = onError;
60+
61+
expect(errors, isNotEmpty);
62+
expect(errors.first, startsWith('Multiple widgets used the same GlobalKey.'));
63+
expect(router.topRoute.name, 'SecondRoute');
64+
});
65+
}

0 commit comments

Comments
 (0)