Fix the issue where DropdownMenu does not gain focus when tapped. (#162874)

Fixes: #162539
Fixes: #162897

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## 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].

<!-- 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
This commit is contained in:
yim 2025-02-20 14:02:07 +08:00 committed by GitHub
parent 19f23f2339
commit c4d269e22b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View File

@ -879,7 +879,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
});
}
void handlePressed(MenuController controller) {
void handlePressed(MenuController controller, {bool focusForKeyboard = true}) {
if (controller.isOpen) {
currentHighlight = null;
controller.close();
@ -889,7 +889,9 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
_enableFilter = false;
}
controller.open();
_internalFocudeNode.requestFocus();
if (focusForKeyboard) {
_internalFocudeNode.requestFocus();
}
}
setState(() {});
}
@ -1047,7 +1049,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
!widget.enabled
? null
: () {
handlePressed(controller);
handlePressed(controller, focusForKeyboard: !canRequestFocus());
},
onChanged: (String text) {
controller.open();

View File

@ -2169,7 +2169,8 @@ void main() {
await tester.pumpAndSettle();
expect(menuAnchor.controller!.isOpen, true);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
// Simulate `TextInputAction.done` on textfield
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pumpAndSettle();
expect(menuAnchor.controller!.isOpen, false);
});
@ -2222,7 +2223,15 @@ void main() {
// Test onSelected on key press
await simulateKeyDownEvent(LogicalKeyboardKey.arrowDown);
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
// On mobile platforms, the TextField cannot gain focus by default; the focus is
// on a FocusNode specifically used for keyboard navigation. Therefore,
// LogicalKeyboardKey.enter should be used.
if (isMobile) {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
} else {
await tester.testTextInput.receiveAction(TextInputAction.done);
}
await tester.pumpAndSettle();
expect(selectionCount, expectedCount);
@ -4011,6 +4020,27 @@ void main() {
expect(textField.textInputAction, TextInputAction.next);
});
// Regression test for https://github.com/flutter/flutter/issues/162539
testWidgets(
'When requestFocusOnTap is true, the TextField should gain focus after being tapped.',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownMenu<TestMenu>(
dropdownMenuEntries: menuChildren,
requestFocusOnTap: true,
),
),
),
);
await tester.tap(find.byType(TextField));
await tester.pumpAndSettle();
final Element textField = tester.firstElement(find.byType(TextField));
expect(Focus.of(textField).hasFocus, isTrue);
},
);
testWidgets('items can be constrainted to be smaller than the text field with menuStyle', (
WidgetTester tester,
) async {