Skip to content

Commit 6c13425

Browse files
Philclaude
andcommitted
Tab past a nested router with a covered page throws nothing, in plain Flutter too. An AnimatedBuilder on the root or nested router follows navigation without "setState during build". A ListTile page in a shell shows when opened by its url. Swapping a dialog and a page, each with a nested router, mid-animation throws nothing. A page declared under two routes opens under the one it is pushed from. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 549b05c commit 6c13425

5 files changed

Lines changed: 346 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
7+
void main() {
8+
// https://github.com/flutter/flutter/issues/169030
9+
// Reported for go_router: a page inside a shell whose content is a bare
10+
// ListTile showed a blank screen when its url was typed, though not when
11+
// it was reached by a tap. A Text in place of the ListTile showed fine.
12+
// Steps: a shell page, a Scaffold around a tabs router whose tab holds a
13+
// stack router of '' and 'bug', each page a bare ListTile. Open the app at
14+
// /test/bug, and in another run at /test and tap through to /test/bug.
15+
// Should: the 'bug' ListTile shows both ways, with no error.
16+
for (final byLink in [true, false]) {
17+
final how = byLink ? 'by its url' : 'by a tap';
18+
testWidgets('A ListTile page in a shell shows when opened $how', (
19+
tester,
20+
) async {
21+
final router = RootStackRouter.build(
22+
routes: [
23+
NamedRouteDef(
24+
name: 'ShellRoute',
25+
path: '/',
26+
builder: (context, _) => Scaffold(
27+
body: GestureDetector(
28+
onTap: () => context.router.navigatePath('/test/bug'),
29+
child: const AutoTabsRouter(routes: [NamedRoute('TestTab')]),
30+
),
31+
),
32+
children: [
33+
NamedRouteDef.shell(
34+
name: 'TestTab',
35+
path: 'test',
36+
children: [
37+
NamedRouteDef(
38+
name: 'HelloRoute',
39+
path: '',
40+
builder: (_, _) =>
41+
const ListTile(title: Text('Hello World')),
42+
),
43+
NamedRouteDef(
44+
name: 'BugRoute',
45+
path: 'bug',
46+
builder: (_, _) => const ListTile(title: Text('bug')),
47+
),
48+
],
49+
),
50+
],
51+
),
52+
],
53+
);
54+
await pumpRouterApp(
55+
tester,
56+
router,
57+
initialLink: byLink ? '/test/bug' : '/test',
58+
);
59+
if (!byLink) {
60+
await tester.tap(find.text('Hello World'));
61+
await tester.pumpAndSettle();
62+
}
63+
64+
expect(tester.takeException(), isNull);
65+
expect(router.currentUrl, '/test/bug');
66+
expect(find.widgetWithText(ListTile, 'bug'), findsOneWidget);
67+
expect(find.text('bug').hitTestable(), findsOneWidget);
68+
});
69+
}
70+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
7+
void main() {
8+
// https://github.com/flutter/flutter/issues/114254
9+
// Reported on Flutter 3.3 and 3.5: Tab from a text field asserted
10+
// "RenderBox was not laid out" when a nested Navigator held a page covered
11+
// by another one that was never laid out.
12+
// Steps, as reported: a column of a TextField and a small nested Navigator
13+
// with two pages; tap the field, press Tab. Then the same in a nested
14+
// AutoRouter opened on its second page.
15+
// Should: no error, and no page removed. The plain Flutter report no
16+
// longer fails either.
17+
testWidgets('Plain Flutter: Tab past a covered nested page throws nothing', (
18+
tester,
19+
) async {
20+
final removed = <Page<Object?>>[];
21+
await tester.pumpWidget(
22+
MaterialApp(
23+
home: Scaffold(
24+
body: Column(
25+
children: [
26+
const TextField(),
27+
SizedBox(
28+
width: 300,
29+
height: 100,
30+
child: Navigator(
31+
requestFocus: false,
32+
pages: const [
33+
MaterialPage<Object>(
34+
name: 'root',
35+
child: SizedBox.expand(),
36+
),
37+
MaterialPage<Object>(
38+
name: 'panel',
39+
child: Text('Then press tab'),
40+
),
41+
],
42+
onDidRemovePage: removed.add,
43+
),
44+
),
45+
],
46+
),
47+
),
48+
),
49+
);
50+
await tester.tap(find.byType(TextField));
51+
await tester.pump();
52+
await tester.sendKeyEvent(.tab);
53+
await tester.pump();
54+
expect((tester.takeException(), removed.length), (null, 0));
55+
});
56+
57+
testWidgets('Tab past a covered page of a nested AutoRouter throws nothing', (
58+
tester,
59+
) async {
60+
final router = RootStackRouter.build(
61+
routes: [
62+
NamedRouteDef(
63+
name: 'HomeRoute',
64+
path: '/',
65+
builder: (_, _) => const Scaffold(
66+
body: Column(
67+
children: [
68+
TextField(),
69+
SizedBox(width: 300, height: 100, child: AutoRouter()),
70+
],
71+
),
72+
),
73+
children: [
74+
NamedRouteDef(
75+
name: 'RootPanelRoute',
76+
path: '',
77+
builder: (_, _) => const SizedBox.expand(),
78+
),
79+
NamedRouteDef(
80+
name: 'PanelRoute',
81+
path: 'panel',
82+
builder: (_, _) => const Text('Then press tab'),
83+
),
84+
],
85+
),
86+
],
87+
);
88+
await pumpRouterApp(tester, router, initialLink: '/panel');
89+
expect(router.innerRouterOf<StackRouter>('HomeRoute')!.stack.length, 2);
90+
await tester.tap(find.byType(TextField));
91+
await tester.pump();
92+
await tester.sendKeyEvent(.tab);
93+
await tester.pump();
94+
expect(tester.takeException(), isNull);
95+
});
96+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
7+
void main() {
8+
// https://github.com/flutter/flutter/issues/182441
9+
// In go_router_builder, a relative route declared under several routes
10+
// always opened under the first of them, even when pushed from another
11+
// one.
12+
// auto_route has no relative route. The same thing is one page declared as
13+
// a child of several routes: here the reviews page, under both the home
14+
// shell and the product shell.
15+
// Steps: open /home, push ReviewsRoute from the home page and go back.
16+
// Push product 1, then push ReviewsRoute from the product page.
17+
// Should: each push opens ReviewsRoute under the page it was pushed from.
18+
testWidgets(
19+
'A page declared under two routes opens under the one it is pushed from',
20+
(tester) async {
21+
final router = RootStackRouter.build(
22+
routes: [
23+
NamedRouteDef.shell(
24+
name: 'HomeRoute',
25+
path: '/home',
26+
children: [
27+
_screen('HomeScreenRoute', ''),
28+
_screen('ReviewsRoute', 'reviews'),
29+
],
30+
),
31+
NamedRouteDef.shell(
32+
name: 'ProductRoute',
33+
path: '/products/:id',
34+
children: [
35+
_screen('ProductScreenRoute', ''),
36+
_screen('ReviewsRoute', 'reviews'),
37+
],
38+
),
39+
],
40+
);
41+
await pumpRouterApp(tester, router, initialLink: '/home');
42+
43+
Future<String> pushReviewsFrom(String page) async {
44+
tester
45+
.element(find.text(page))
46+
.router
47+
.push(const NamedRoute('ReviewsRoute'))
48+
.ignore();
49+
await tester.pumpAndSettle();
50+
expect(find.text('ReviewsRoute'), findsOneWidget);
51+
return router.currentUrl;
52+
}
53+
54+
final fromHome = await pushReviewsFrom('HomeScreenRoute');
55+
await router.maybePopTop();
56+
await tester.pumpAndSettle();
57+
router.push(const NamedRoute('ProductRoute', params: {'id': 1})).ignore();
58+
await tester.pumpAndSettle();
59+
final fromProduct = await pushReviewsFrom('ProductScreenRoute');
60+
61+
expect(tester.takeException(), isNull);
62+
expect((fromHome, fromProduct), ('/home/reviews', '/products/1/reviews'));
63+
expect(router.stack.map((page) => page.name), [
64+
'HomeRoute',
65+
'ProductRoute',
66+
]);
67+
},
68+
);
69+
}
70+
71+
NamedRouteDef _screen(String name, String path) =>
72+
.new(name: name, path: path, builder: (_, _) => Text(name));

‎auto_route/test/url_title.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:auto_route/auto_route.dart';
2+
import 'package:material_ui/material_ui.dart';
3+
4+
/// Shows the url of a router, rebuilt by an [AnimatedBuilder] that listens to
5+
/// the router, with the key 'title'.
6+
class UrlTitle extends StatelessWidget {
7+
const new(this._router, {super.key});
8+
9+
/// The router whose url is shown, and listened to.
10+
final RoutingController _router;
11+
12+
@override
13+
Widget build(BuildContext context) => AnimatedBuilder(
14+
animation: _router,
15+
builder: (_, _) => Text(_router.currentUrl, key: const Key('title')),
16+
);
17+
}

0 commit comments

Comments
 (0)