Skip to content

Commit c65cab8

Browse files
authored
[DropdownMenu] Close menu after editing is complete (#130710)
Fixes: #130674 Before: The dropdown menu was not closed if empty text was provided After: The dropdown menu is closed even if empty text is provided https://github.com/flutter/flutter/assets/61322712/fccac501-9fca-4f60-8a94-abfc50552ec9 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent 43afac1 commit c65cab8

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/flutter/lib/src/material/dropdown_menu.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
612612
if (!widget.enableSearch) {
613613
currentHighlight = null;
614614
}
615-
if (_textEditingController.text.isNotEmpty) {
616-
controller.close();
617-
}
615+
controller.close();
618616
},
619617
onTap: () {
620618
handlePressed(controller);

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,34 @@ void main() {
10501050
expect(controller.text, 'New Item');
10511051
});
10521052

1053+
testWidgets('The menu should be closed after text editing is complete', (WidgetTester tester) async {
1054+
final ThemeData themeData = ThemeData();
1055+
final TextEditingController controller = TextEditingController();
1056+
await tester.pumpWidget(MaterialApp(
1057+
theme: themeData,
1058+
home: Scaffold(
1059+
body: DropdownMenu<TestMenu>(
1060+
requestFocusOnTap: true,
1061+
enableFilter: true,
1062+
dropdownMenuEntries: menuChildren,
1063+
controller: controller,
1064+
),
1065+
),
1066+
));
1067+
// Access the MenuAnchor
1068+
final MenuAnchor menuAnchor = tester.widget<MenuAnchor>(find.byType(MenuAnchor));
1069+
1070+
// Open the menu
1071+
await tester.tap(find.byType(DropdownMenu<TestMenu>));
1072+
await tester.pumpAndSettle();
1073+
expect(menuAnchor.controller!.isOpen, true);
1074+
1075+
// Simulate `TextInputAction.done` on textfield
1076+
await tester.testTextInput.receiveAction(TextInputAction.done);
1077+
await tester.pumpAndSettle();
1078+
expect(menuAnchor.controller!.isOpen, false);
1079+
});
1080+
10531081
testWidgets('The onSelected gets called only when a selection is made', (WidgetTester tester) async {
10541082
int selectionCount = 0;
10551083

0 commit comments

Comments
 (0)