Skip to content

Commit f772411

Browse files
Philclaude
andcommitted
fix: a text field in a nested router keeps focus on the web with semantics on (flutter/flutter#194873)
With semantics on, a text field inside a nested AutoRouter lost the browser's focus as its Autocomplete options opened or closed: Chrome took no typing at all, Firefox lost focus on the first non-matching letter or a delete. Cause: auto_route wraps each nested Navigator in Semantics(container: true) (52eb485, the workaround for flutter/flutter#150978). With implicit child nodes, that container gains an extra semantics node above the field while the options overlay shows, and drops it when the overlay closes. The focused field changes semantics parents each time, the web engine moves its <input> in the DOM, and the browser blurs it. Fix: explicitChildNodes: true on that wrapper. This narrows the #150978 workaround rather than removing it; the container stays and #150978's tests stay green. The headless test checks the field's semantics parent chain stays the same as the options open, close and open again; it fails on the parent (an extra node at "options opened") and, in plain Flutter, tells container-only (fails) from no wrapper and container + explicitChildNodes (both pass). The integration test runs the same steps and, on the web, checks document.activeElement stays the INPUT; under flutter drive in headless Chrome it fails without the fix (FLUTTER-VIEW) and passes with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d39adac commit f772411

8 files changed

Lines changed: 287 additions & 2 deletions

File tree

‎auto_route/example.old/integration_test/all_tests.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ import 'nested_result_test.dart' as nested_result;
7878
import 'nested_route_aware_test.dart' as nested_route_aware;
7979
import 'nested_route_fragment_test.dart' as nested_route_fragment;
8080
import 'nested_router_semantics_test.dart' as nested_router_semantics;
81+
import 'nested_router_text_field_focus_test.dart'
82+
as nested_router_text_field_focus;
8183
import 'nested_shell_back_test.dart' as nested_shell_back;
8284
import 'nested_tab_bar_reevaluation_test.dart' as nested_tab_bar_reevaluation;
8385
import 'non_ascii_url_test.dart' as non_ascii_url;
@@ -279,6 +281,7 @@ void main() {
279281
nested_route_aware.main();
280282
nested_route_fragment.main();
281283
nested_router_semantics.main();
284+
nested_router_text_field_focus.main();
282285
nested_shell_back.main();
283286
nested_tab_bar_reevaluation.main();
284287
non_ascii_url.main();
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import 'package:auto_route/auto_route.dart';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:integration_test/integration_test.dart';
5+
import 'package:material_ui/material_ui.dart';
6+
7+
import 'support/app.dart';
8+
import 'support/autocomplete_state_field.dart';
9+
import 'support/browser_focus.dart'
10+
if (dart.library.js_interop) 'support/browser_focus_web.dart';
11+
12+
// A text field in a nested router keeps focus while its Autocomplete options
13+
// open and close, with semantics on.
14+
// Reproduces https://github.com/flutter/flutter/issues/194873 in auto_route.
15+
// Headless test: auto_route/test/nested_router_semantics_test.dart
16+
void main() {
17+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
18+
19+
group('nested router text field focus', () {
20+
// Steps: with semantics on, open a page beside a semantics container
21+
// that shows an AutoRouter, which shows another AutoRouter, which shows
22+
// an Autocomplete field. Tap the field (its options open), type a letter
23+
// no option holds (they close), then delete it (they open again).
24+
// Should: the field keeps its semantics parents, so on the web the
25+
// browser's focus stays on the field's <input>.
26+
testWidgets('a nested text field keeps focus', (tester) async {
27+
final semantics = tester.ensureSemantics();
28+
final router = RootStackRouter.build(
29+
routes: [
30+
AutoRoute(
31+
path: '/',
32+
page: PageInfo(
33+
'OuterShellRoute',
34+
builder: (_) => Scaffold(
35+
body: Row(
36+
children: [
37+
Semantics(
38+
container: true,
39+
label: 'Shell navigation',
40+
child: const SizedBox(width: 80, child: Text('Nav')),
41+
),
42+
const Expanded(child: AutoRouter()),
43+
],
44+
),
45+
),
46+
),
47+
children: [
48+
AutoRoute(
49+
path: '',
50+
page: PageInfo(
51+
'InnerShellRoute',
52+
builder: (_) => const AutoRouter(),
53+
),
54+
children: [
55+
AutoRoute(
56+
path: '',
57+
page: PageInfo(
58+
'StateFieldRoute',
59+
builder: (_) => const AutocompleteStateField(),
60+
),
61+
),
62+
],
63+
),
64+
],
65+
),
66+
],
67+
);
68+
await pumpRouterApp(tester, router);
69+
70+
List<int> fieldParents() {
71+
final parents = <int>[];
72+
for (
73+
var node = tester.getSemantics(find.byType(EditableText)).parent;
74+
node != null;
75+
node = node.parent
76+
) {
77+
parents.add(node.id);
78+
}
79+
return parents;
80+
}
81+
82+
final unfocused = fieldParents();
83+
// Off the web there is no DOM, so only the semantics parents count.
84+
Future<void> expectStillFocused(
85+
String step, {
86+
required bool areOptionsShown,
87+
}) async {
88+
await tester.pumpAndSettle();
89+
expect(
90+
find.text('Texas'),
91+
areOptionsShown ? findsOneWidget : findsNothing,
92+
);
93+
expect(fieldParents(), unfocused, reason: step);
94+
if (kIsWeb) expect(browserFocusedTag(), 'INPUT', reason: step);
95+
}
96+
97+
await tester.tap(find.byType(TextField));
98+
await expectStillFocused('options opened', areOptionsShown: true);
99+
await tester.enterText(find.byType(TextField), 'q');
100+
await expectStillFocused('options closed', areOptionsShown: false);
101+
await tester.enterText(find.byType(TextField), '');
102+
await expectStillFocused('options opened again', areOptionsShown: true);
103+
semantics.dispose();
104+
});
105+
});
106+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:material_ui/material_ui.dart';
2+
3+
/// An Autocomplete text field labeled 'State', offering Texas, Tennessee and
4+
/// New York.
5+
///
6+
/// Focusing it with an empty text shows all three options; text no option
7+
/// holds, such as 'q', hides them. flutter/flutter#194873's tests use it to
8+
/// open and close the options overlay above a focused field.
9+
class AutocompleteStateField extends StatelessWidget {
10+
/// Creates the field.
11+
const new({super.key});
12+
13+
@override
14+
Widget build(BuildContext context) => Center(
15+
child: SizedBox(
16+
width: 300,
17+
child: Autocomplete<String>(
18+
optionsBuilder: (value) => ['Texas', 'Tennessee', 'New York'].where(
19+
(option) => option.toLowerCase().contains(value.text.toLowerCase()),
20+
),
21+
fieldViewBuilder: (context, controller, focusNode, _) => TextField(
22+
controller: controller,
23+
focusNode: focusNode,
24+
decoration: const InputDecoration(labelText: 'State'),
25+
),
26+
),
27+
),
28+
);
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// The tag name of the element that has the browser's focus, such as `INPUT`
2+
/// or `FLUTTER-VIEW`, or null off the web.
3+
String? browserFocusedTag() => null;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'dart:js_interop';
2+
import 'dart:js_interop_unsafe';
3+
4+
/// The tag name of the element that has the browser's focus, such as `INPUT`
5+
/// or `FLUTTER-VIEW`.
6+
///
7+
/// With semantics on, a focused Flutter text field is a DOM `<input>` that
8+
/// the engine keeps focused while the user types.
9+
// Matches the stub in browser_focus.dart, which has no browser to read.
10+
String? browserFocusedTag() {
11+
final document = globalContext.getProperty<JSObject>('document'.toJS);
12+
final focused = document.getProperty<JSObject?>('activeElement'.toJS);
13+
return focused?.getProperty<JSString>('tagName'.toJS).toDart;
14+
}

‎auto_route/lib/src/router/widgets/auto_route_navigator.dart‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,17 @@ class AutoRouteNavigatorState extends State<AutoRouteNavigator> {
214214
// Flutter drops the semantics of widgets painted before a nested
215215
// Navigator when their common parent merges its nodes upward
216216
// (flutter/flutter#150978). A container node of its own keeps the
217-
// Navigator's route from blocking them.
218-
child: Semantics(container: true, child: navigator),
217+
// Navigator's route from blocking them. Its child nodes stay
218+
// explicit: otherwise the container gains and drops a node as an
219+
// overlay entry such as Autocomplete's options opens and closes, a
220+
// focused text field changes semantics parents, and the web engine
221+
// moves its <input> in the DOM, which blurs it
222+
// (flutter/flutter#194873).
223+
child: Semantics(
224+
container: true,
225+
explicitChildNodes: true,
226+
child: navigator,
227+
),
219228
),
220229
);
221230
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:material_ui/material_ui.dart';
2+
3+
/// An Autocomplete text field labeled 'State', offering Texas, Tennessee and
4+
/// New York.
5+
///
6+
/// Focusing it with an empty text shows all three options; text no option
7+
/// holds, such as 'q', hides them. flutter/flutter#194873's tests use it to
8+
/// open and close the options overlay above a focused field.
9+
class AutocompleteStateField extends StatelessWidget {
10+
/// Creates the field.
11+
const new({super.key});
12+
13+
@override
14+
Widget build(BuildContext context) => Center(
15+
child: SizedBox(
16+
width: 300,
17+
child: Autocomplete<String>(
18+
optionsBuilder: (value) => ['Texas', 'Tennessee', 'New York'].where(
19+
(option) => option.toLowerCase().contains(value.text.toLowerCase()),
20+
),
21+
fieldViewBuilder: (context, controller, focusNode, _) => TextField(
22+
controller: controller,
23+
focusNode: focusNode,
24+
decoration: const InputDecoration(labelText: 'State'),
25+
),
26+
),
27+
),
28+
);
29+
}

‎auto_route/test/nested_router_semantics_test.dart‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:auto_route/auto_route.dart';
22
import 'package:flutter_test/flutter_test.dart';
33
import 'package:material_ui/material_ui.dart';
44

5+
import 'autocomplete_state_field.dart';
56
import 'router_test_utils.dart';
67

78
void main() {
@@ -101,4 +102,95 @@ void main() {
101102
expect(find.bySemanticsLabel('c2'), findsOneWidget);
102103
semantics.dispose();
103104
});
105+
// Reproduces https://github.com/flutter/flutter/issues/194873 in
106+
// auto_route. Steps: with semantics on, a page beside a semantics container
107+
// shows an AutoRouter, which shows another AutoRouter, which shows an
108+
// Autocomplete text field. Focus the field (its options open), type a
109+
// letter no option holds (they close), then delete it (they open again).
110+
// Happened: the field's semantics node changed parents each time the
111+
// options opened or closed. The web engine then moves the focused <input>
112+
// in the DOM, which blurs it: Chrome took no typing, Firefox lost focus on
113+
// the first non-matching letter. The container node auto_route put around
114+
// each nested Navigator for flutter/flutter#150978 grew an extra child
115+
// node while the options showed, and dropped it when they closed.
116+
// Should: the field keeps the same semantics parents throughout.
117+
testWidgets('A text field in a nested router keeps its semantics parents', (
118+
tester,
119+
) async {
120+
final semantics = tester.ensureSemantics();
121+
final router = RootStackRouter.build(
122+
routes: [
123+
AutoRoute(
124+
path: '/',
125+
page: PageInfo(
126+
'OuterShellRoute',
127+
builder: (_) => Scaffold(
128+
body: Row(
129+
children: [
130+
Semantics(
131+
container: true,
132+
label: 'Shell navigation',
133+
child: const SizedBox(width: 80, child: Text('Nav')),
134+
),
135+
const Expanded(child: AutoRouter()),
136+
],
137+
),
138+
),
139+
),
140+
children: [
141+
AutoRoute(
142+
path: '',
143+
page: PageInfo(
144+
'InnerShellRoute',
145+
builder: (_) => const AutoRouter(),
146+
),
147+
children: [
148+
AutoRoute(
149+
path: '',
150+
page: PageInfo(
151+
'StateFieldRoute',
152+
builder: (_) => const AutocompleteStateField(),
153+
),
154+
),
155+
],
156+
),
157+
],
158+
),
159+
],
160+
);
161+
await pumpRouterApp(tester, router);
162+
163+
List<int> fieldParents() {
164+
final parents = <int>[];
165+
for (
166+
var node = tester.getSemantics(find.byType(EditableText)).parent;
167+
node != null;
168+
node = node.parent
169+
) {
170+
parents.add(node.id);
171+
}
172+
return parents;
173+
}
174+
175+
final unfocused = fieldParents();
176+
Future<void> expectSameParents(
177+
String step, {
178+
required bool areOptionsShown,
179+
}) async {
180+
await tester.pump();
181+
expect(
182+
find.text('Texas'),
183+
areOptionsShown ? findsOneWidget : findsNothing,
184+
);
185+
expect(fieldParents(), unfocused, reason: step);
186+
}
187+
188+
await tester.tap(find.byType(TextField));
189+
await expectSameParents('options opened', areOptionsShown: true);
190+
await tester.enterText(find.byType(TextField), 'q');
191+
await expectSameParents('options closed', areOptionsShown: false);
192+
await tester.enterText(find.byType(TextField), '');
193+
await expectSameParents('options opened again', areOptionsShown: true);
194+
semantics.dispose();
195+
});
104196
}

0 commit comments

Comments
 (0)