Unfocus search anchor bar when the view is closed (#178910)

- Fix https://github.com/flutter/flutter/issues/178719
- See
https://github.com/flutter/flutter/issues/178719#issuecomment-3561809294
for details of debugging and proposal.

<details open>
<summary>Demo (after the fix)</summary>


https://github.com/user-attachments/assets/cecb470f-d98c-47b0-a34d-fc858cfb0dd4
</details>


## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

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.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

Signed-off-by: huycozy <huy@nevercode.io>
This commit is contained in:
Huy 2025-12-02 18:36:25 +07:00 committed by GitHub
parent 4b6e0bdcfa
commit 14063cfd0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 1 deletions

View File

@ -695,6 +695,14 @@ class _SearchViewRoute extends PopupRoute<_SearchViewRoute> {
updateTweens(anchorKey.currentContext!);
toggleVisibility?.call();
viewOnClose?.call();
// Unfocus the anchor to prevent the Enter key from triggering unwanted
// actions (like route pops) when the view closes and focus returns to
// the anchor's search bar.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (anchorKey.currentContext != null) {
FocusScope.of(anchorKey.currentContext!).unfocus();
}
});
return super.didPop(result);
}

View File

@ -995,9 +995,75 @@ void main() {
await tester.testTextInput.receiveAction(TextInputAction.done);
expect(onSubmittedCalled, 1);
expect(controller.isOpen, false);
});
// Regression test for https://github.com/flutter/flutter/issues/178719.
testWidgets('SearchAnchor.bar anchor loses focus after view closes', (WidgetTester tester) async {
final controller = SearchController();
addTearDown(controller.dispose);
var onSubmittedCalled = 0;
await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Center(
child: Material(
child: SearchAnchor.bar(
searchController: controller,
onSubmitted: (String value) {
setState(() {
onSubmittedCalled++;
});
controller.closeView(value);
},
suggestionsBuilder: (BuildContext context, SearchController controller) {
return <Widget>[];
},
),
),
);
},
),
),
);
// Tap to open the search view.
await tester.tap(find.byType(SearchBar));
await tester.pumpAndSettle();
expect(controller.isOpen, true);
// Find the anchor SearchBar's TextField.
final Finder anchorTextField = find.descendant(
of: find.byType(SearchBar).first,
matching: find.byType(TextField),
);
// Find the view SearchBar's TextField.
final Finder viewTextField = find.descendant(
of: findViewContent(),
matching: find.byType(TextField),
);
// View SearchBar should have focus.
expect(tester.widget<TextField>(viewTextField).focusNode?.hasFocus, isTrue);
// Close the view (use receiveAction because sendKeyEvent
// doesn't trigger onSubmitted in tests - the text input action needs to
// be sent directly to properly simulate the IME behavior).
await tester.testTextInput.receiveAction(TextInputAction.done);
expect(onSubmittedCalled, 2);
await tester.pumpAndSettle();
expect(onSubmittedCalled, 1);
expect(controller.isOpen, false);
// After search view is closed, anchor SearchBar should NOT have focus.
expect(tester.widget<TextField>(anchorTextField).focusNode?.hasFocus, isFalse);
// Simulate the IME behavior via input action again .
// It should not trigger onSubmitted because field is unfocused
// and the text input connection is closed.
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
expect(onSubmittedCalled, 1); // Still 1, not incremented.
});
testWidgets('hintStyle can override textStyle for hintText', (WidgetTester tester) async {