|
| 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