From c65cab8fa3c13c481849e5fbaed572fc3e53c08b Mon Sep 17 00:00:00 2001 From: Sabin Neupane <61322712+sabin26@users.noreply.github.com> Date: Tue, 25 Jul 2023 00:00:53 +0545 Subject: [PATCH] [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].* --- .../lib/src/material/dropdown_menu.dart | 4 +-- .../test/material/dropdown_menu_test.dart | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 4bf3cee597..51a972a339 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -612,9 +612,7 @@ class _DropdownMenuState extends State> { if (!widget.enableSearch) { currentHighlight = null; } - if (_textEditingController.text.isNotEmpty) { - controller.close(); - } + controller.close(); }, onTap: () { handlePressed(controller); diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index d61d1764a8..a721cfdb87 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -1050,6 +1050,34 @@ void main() { expect(controller.text, 'New Item'); }); + testWidgets('The menu should be closed after text editing is complete', (WidgetTester tester) async { + final ThemeData themeData = ThemeData(); + final TextEditingController controller = TextEditingController(); + await tester.pumpWidget(MaterialApp( + theme: themeData, + home: Scaffold( + body: DropdownMenu( + requestFocusOnTap: true, + enableFilter: true, + dropdownMenuEntries: menuChildren, + controller: controller, + ), + ), + )); + // Access the MenuAnchor + final MenuAnchor menuAnchor = tester.widget(find.byType(MenuAnchor)); + + // Open the menu + await tester.tap(find.byType(DropdownMenu)); + await tester.pumpAndSettle(); + expect(menuAnchor.controller!.isOpen, true); + + // Simulate `TextInputAction.done` on textfield + await tester.testTextInput.receiveAction(TextInputAction.done); + await tester.pumpAndSettle(); + expect(menuAnchor.controller!.isOpen, false); + }); + testWidgets('The onSelected gets called only when a selection is made', (WidgetTester tester) async { int selectionCount = 0;