|
| 1 | +import 'package:auto_route/auto_route.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:material_ui/material_ui.dart'; |
| 4 | + |
| 5 | +import '../router_test_utils.dart'; |
| 6 | +import '../url_title.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + // https://github.com/flutter/flutter/issues/110687 |
| 10 | + // Reported for go_router: an AnimatedBuilder listening to the router, in a |
| 11 | + // scaffold around the pages, threw "setState() or markNeedsBuild() called |
| 12 | + // during build" on every navigation, and its text did not always update. |
| 13 | + // Steps: a shell page whose AppBar title is |
| 14 | + // AnimatedBuilder(animation: router) showing the url, with the pages in |
| 15 | + // its nested AutoRouter; go from page 1 to page 2 and back, once listening |
| 16 | + // to the root router and once to the nested one. |
| 17 | + // Should: no error, and the title shows each new url. |
| 18 | + for (final listensToRoot in [true, false]) { |
| 19 | + testWidgets( |
| 20 | + 'An AnimatedBuilder on the ${listensToRoot ? 'root' : 'nested'} router ' |
| 21 | + 'follows navigation without errors', |
| 22 | + (tester) async { |
| 23 | + final router = RootStackRouter.build( |
| 24 | + routes: [ |
| 25 | + NamedRouteDef( |
| 26 | + name: 'ShellRoute', |
| 27 | + path: '/', |
| 28 | + builder: (context, _) { |
| 29 | + final listened = listensToRoot ? context.router : null; |
| 30 | + return Scaffold( |
| 31 | + appBar: AppBar( |
| 32 | + title: listened == null |
| 33 | + ? Builder( |
| 34 | + builder: (context) { |
| 35 | + final nested = context.innerRouterOf<StackRouter>( |
| 36 | + 'ShellRoute', |
| 37 | + ); |
| 38 | + return nested == null |
| 39 | + ? const SizedBox() |
| 40 | + : UrlTitle(nested); |
| 41 | + }, |
| 42 | + ) |
| 43 | + : UrlTitle(listened), |
| 44 | + ), |
| 45 | + body: const AutoRouter(), |
| 46 | + ); |
| 47 | + }, |
| 48 | + children: [ |
| 49 | + NamedRouteDef( |
| 50 | + name: 'Page1Route', |
| 51 | + path: '', |
| 52 | + builder: (context, _) => TextButton( |
| 53 | + onPressed: () => |
| 54 | + context.router.navigate(const NamedRoute('Page2Route')), |
| 55 | + child: const Text('Go to page 2'), |
| 56 | + ), |
| 57 | + ), |
| 58 | + NamedRouteDef( |
| 59 | + name: 'Page2Route', |
| 60 | + path: 'page2', |
| 61 | + builder: (context, _) => TextButton( |
| 62 | + onPressed: () => |
| 63 | + context.router.navigate(const NamedRoute('Page1Route')), |
| 64 | + child: const Text('Go to home page'), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ], |
| 68 | + ), |
| 69 | + ], |
| 70 | + ); |
| 71 | + await pumpRouterConfigApp(tester, router.config()); |
| 72 | + final titles = <String>[]; |
| 73 | + final errors = <Object?>[]; |
| 74 | + void readTitle() { |
| 75 | + errors.add(tester.takeException()); |
| 76 | + titles.add(tester.widget<Text>(find.byKey(const Key('title'))).data!); |
| 77 | + } |
| 78 | + |
| 79 | + readTitle(); |
| 80 | + await tester.tap(find.text('Go to page 2')); |
| 81 | + await tester.pumpAndSettle(); |
| 82 | + readTitle(); |
| 83 | + await tester.tap(find.text('Go to home page')); |
| 84 | + await tester.pumpAndSettle(); |
| 85 | + readTitle(); |
| 86 | + expect(errors, [null, null, null]); |
| 87 | + expect(titles.join(' | '), '/ | /page2 | /'); |
| 88 | + }, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments