Skip to content

Commit e872aea

Browse files
authored
[go_router] Keep web autocomplete focused in shell routes (flutter#13348)
## Summary Fixes flutter/flutter#194873. Opening an `Autocomplete` overlay inside a `ShellRoute` can add a semantics parent above the focused field. On Flutter web, that detaches the field's DOM input and drops focus. ```diff ShellRoute navigator semantics - Semantics(container: true, child: navigator) + Semantics(container: true, explicitChildNodes: true, child: navigator) ``` The change keeps the field's semantics ancestry stable while retaining the shell navigator boundary added to preserve shell chrome semantics. It includes a regression test and a pending patch changelog entry for the batched release workflow. ## Evidence - **Before:** The regression test fails on unmodified `main`: the focused field's semantics ancestor IDs change from `[6, 4, 3, 2, 1, 0]` to `[6, 4, 7, 3, 2, 1, 0]` when options open. In a Chrome release web repro with published `go_router` 18.1.1, typing `t`, `e`, `x` leaves the field empty and browser focus leaves the input. - **After:** The regression test passes. In the same Chrome repro with the patched package, the field retains browser focus and contains `tex`; shell navigation remains in the semantics tree. - **Checks:** `format`, `analyze`, `dart-test`, `validate`, and `publish-check` through `flutter_plugin_tools` pass for `go_router`. The package and example tests pass. ## Merge Danger **Door:** Two-way. The patch can be reverted without data migration. **Blast Radius:** ShellRoute semantics trees. Explicit child nodes can affect accessibility traversal and announcements for routed content; existing shell chrome semantics tests and the new overlay regression test pass. Human review should include accessibility behavior on supported platforms. ## Pre-Review Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran [the auto-formatter]. - [x] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1]. - [ ] I updated/added any relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1]. - [x] All existing and new tests are passing. No public API or doc comments changed; the bug fix does not introduce a new API. `go_router` uses pending changelog files for batched releases; this PR adds a patch entry there. This PR was prepared with AI assistance and reviewed by the contributor. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates [semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions [repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
1 parent ea7e031 commit e872aea

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎packages/go_router/lib/src/builder.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ class _CustomNavigatorState extends State<_CustomNavigator> {
505505
// semantics of shell chrome painted before this navigator (e.g. a
506506
// side rail in a Row-based ShellRoute shell). See
507507
// https://github.com/flutter/flutter/issues/135656.
508-
child: widget.isShellNavigator ? Semantics(container: true, child: navigator) : navigator,
508+
child: widget.isShellNavigator
509+
? Semantics(container: true, explicitChildNodes: true, child: navigator)
510+
: navigator,
509511
),
510512
);
511513
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes web text fields losing focus when an autocomplete overlay opens inside a shell route.
3+
version: patch

‎packages/go_router/test/builder_test.dart‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/rendering.dart' show SemanticsNode;
56
import 'package:flutter_test/flutter_test.dart';
67
import 'package:go_router/go_router.dart';
78
import 'package:material_ui/material_ui.dart';
@@ -434,6 +435,81 @@ void main() {
434435
});
435436

436437
group('Shell navigator semantics boundary', () {
438+
testWidgets('Opening autocomplete options does not reparent the focused field', (
439+
WidgetTester tester,
440+
) async {
441+
final SemanticsHandle semantics = tester.ensureSemantics();
442+
// Widget tests verify semantics handles before addTearDown callbacks run.
443+
try {
444+
final router = GoRouter(
445+
routes: <RouteBase>[
446+
ShellRoute(
447+
builder: (BuildContext context, GoRouterState state, Widget child) => child,
448+
routes: <RouteBase>[
449+
GoRoute(
450+
path: '/',
451+
builder: (BuildContext context, GoRouterState state) => Scaffold(
452+
body: Column(
453+
children: <Widget>[
454+
const TextField(key: ValueKey<String>('previous-field')),
455+
Autocomplete<String>(
456+
optionsBuilder: (TextEditingValue value) =>
457+
<String>['Texas', 'Tennessee'].where(
458+
(String option) =>
459+
option.toLowerCase().contains(value.text.toLowerCase()),
460+
),
461+
fieldViewBuilder:
462+
(
463+
BuildContext context,
464+
TextEditingController controller,
465+
FocusNode focusNode,
466+
VoidCallback onFieldSubmitted,
467+
) => TextField(
468+
key: const ValueKey<String>('state-field'),
469+
controller: controller,
470+
focusNode: focusNode,
471+
),
472+
),
473+
],
474+
),
475+
),
476+
),
477+
],
478+
),
479+
],
480+
);
481+
addTearDown(router.dispose);
482+
483+
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
484+
await tester.pumpAndSettle();
485+
final Finder stateField = find.descendant(
486+
of: find.byKey(const ValueKey<String>('state-field')),
487+
matching: find.byType(EditableText),
488+
);
489+
List<int> ancestors() => <int>[
490+
for (
491+
SemanticsNode? node = tester.getSemantics(stateField);
492+
node != null;
493+
node = node.parent
494+
)
495+
node.id,
496+
];
497+
498+
await tester.tap(find.byKey(const ValueKey<String>('previous-field')));
499+
await tester.pumpAndSettle();
500+
final List<int> before = ancestors();
501+
502+
await tester.tap(stateField);
503+
await tester.pump();
504+
expect(find.text('Texas'), findsOneWidget);
505+
// A new semantics parent detaches the focused web input from the DOM.
506+
final List<int> after = ancestors();
507+
expect(after, before);
508+
} finally {
509+
semantics.dispose();
510+
}
511+
});
512+
437513
testWidgets('Chrome painted before a ShellRoute Navigator stays in the '
438514
'semantics tree alongside routed content', (WidgetTester tester) async {
439515
final SemanticsHandle semantics = tester.ensureSemantics();

0 commit comments

Comments
 (0)