From e13e9a08e1080f1754a984ebd5b4cb8faf1bfaf6 Mon Sep 17 00:00:00 2001 From: PurplePolyhedron <120297255+PurplePolyhedron@users.noreply.github.com> Date: Thu, 22 Aug 2024 01:00:15 +0800 Subject: [PATCH] Disable Dropdown menu search during keyboard navigation (#152378) fixes https://github.com/flutter/flutter/issues/152375 PR https://github.com/flutter/flutter/pull/147294 fix keyboard navigation issue https://github.com/flutter/flutter/issues/147253 by prioritising current selection in default search. This revert PR https://github.com/flutter/flutter/pull/147294 and fix the issue by disabling `DropdownMenu` search during keyboard navigation instead. also fixes https://github.com/flutter/flutter/issues/151878 as it is a regression caused by PR https://github.com/flutter/flutter/pull/147294. Reason for reverting https://github.com/flutter/flutter/pull/147294 : it doesn't fix the keyboard navigation issue if a custom searchCallback(Issue https://github.com/flutter/flutter/issues/152375) is provided, and "prioritising current selection" is ambiguous for a custom searchCallback, so it would be tricky to have searchCallback behave the same way as the default search. Additionally the original search function was agnostic about the state of `DropdownMenu` other than the entry value type, might be good to keep it that way unless needed. --- .../lib/src/material/dropdown_menu.dart | 8 +- .../test/material/dropdown_menu_test.dart | 108 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 856ce5824c..8ae003a424 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -489,6 +489,7 @@ class _DropdownMenuState extends State> { late List buttonItemKeys; final MenuController _controller = MenuController(); bool _enableFilter = false; + late bool _enableSearch; late List> filteredEntries; List? _initialMenu; int? currentHighlight; @@ -504,6 +505,7 @@ class _DropdownMenuState extends State> { } else { _localTextEditingController = TextEditingController(); } + _enableSearch = widget.enableSearch; filteredEntries = widget.dropdownMenuEntries; buttonItemKeys = List.generate(filteredEntries.length, (int index) => GlobalKey()); _menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry entry) => entry.enabled); @@ -543,6 +545,7 @@ class _DropdownMenuState extends State> { } if (oldWidget.enableSearch != widget.enableSearch) { if (!widget.enableSearch) { + _enableSearch = widget.enableSearch; currentHighlight = null; } } @@ -698,6 +701,7 @@ class _DropdownMenuState extends State> { return; } _enableFilter = false; + _enableSearch = false; currentHighlight ??= 0; currentHighlight = (currentHighlight! - 1) % filteredEntries.length; while (!filteredEntries[currentHighlight!].enabled) { @@ -717,6 +721,7 @@ class _DropdownMenuState extends State> { return; } _enableFilter = false; + _enableSearch = false; currentHighlight ??= -1; currentHighlight = (currentHighlight! + 1) % filteredEntries.length; while (!filteredEntries[currentHighlight!].enabled) { @@ -757,7 +762,7 @@ class _DropdownMenuState extends State> { filteredEntries = widget.dropdownMenuEntries; } - if (widget.enableSearch) { + if (_enableSearch) { if (widget.searchCallback != null) { currentHighlight = widget.searchCallback!(filteredEntries, _localTextEditingController!.text); } else { @@ -856,6 +861,7 @@ class _DropdownMenuState extends State> { setState(() { filteredEntries = widget.dropdownMenuEntries; _enableFilter = widget.enableFilter; + _enableSearch = widget.enableSearch; }); }, inputFormatters: widget.inputFormatters, diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 2bcbc9e20c..98b04eefa0 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -961,6 +961,114 @@ void main() { expect(item2material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); }, variant: TargetPlatformVariant.desktop()); + // Regression test for https://github.com/flutter/flutter/issues/152375. + testWidgets('Down key and up key can navigate on desktop platforms when a label text contains ' + 'another label text using customized search algorithm', (WidgetTester tester) async { + final ThemeData themeData = ThemeData(); + await tester.pumpWidget(MaterialApp( + theme: themeData, + home: Scaffold( + body: DropdownMenu( + searchCallback: (List> entries, String query) { + if (query.isEmpty) { + return null; + } + final int index = entries.indexWhere( + (DropdownMenuEntry entry) => entry.label.contains(query), + ); + return index != -1 ? index : null; + }, + dropdownMenuEntries: const >[ + DropdownMenuEntry( + value: 0, + label: 'ABC' + ), + DropdownMenuEntry( + value: 1, + label: 'AB' + ), + DropdownMenuEntry( + value: 2, + label: 'ABCD' + ), + ], + ), + ), + )); + + await tester.tap(find.byType(DropdownMenu)); + await tester.pump(); + + final Finder button0Material = find.descendant( + of: find.widgetWithText(MenuItemButton, 'ABC').last, + matching: find.byType(Material), + ); + final Finder button1Material = find.descendant( + of: find.widgetWithText(MenuItemButton, 'AB').last, + matching: find.byType(Material), + ); + final Finder button2Material = find.descendant( + of: find.widgetWithText(MenuItemButton, 'ABCD').last, + matching: find.byType(Material), + ); + + // Press down key three times, the highlight should move to the next item each time. + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + Material item0Material = tester.widget(button0Material); + expect(item0Material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); + + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + Material item1Material = tester.widget(button1Material); + expect(item1Material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); + + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + final Material item2Material = tester.widget(button2Material); + expect(item2Material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); + + // Press up key two times, the highlight should up each time. + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + item1Material = tester.widget(button1Material); + expect(item1Material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); + + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + item0Material = tester.widget(button0Material); + expect(item0Material.color, themeData.colorScheme.onSurface.withOpacity(0.12)); + + }, variant: TargetPlatformVariant.desktop()); + + // Regression test for https://github.com/flutter/flutter/issues/152375. + testWidgets('Searching can hightlight entry after keyboard navigation', (WidgetTester tester) async { + final ThemeData themeData = ThemeData(); + await tester.pumpWidget(MaterialApp( + theme: themeData, + home: Scaffold( + body: DropdownMenu( + dropdownMenuEntries: menuChildren, + ), + ), + )); + + // Open the menu and highlight the first item. + await tester.tap(find.byType(DropdownMenu)); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + // Search for the last item. + await tester.enterText(find.byType(TextField).first, menuChildren.last.label); + await tester.pump(); + final Finder buttonMaterial = find.descendant( + of: find.widgetWithText(MenuItemButton, menuChildren.last.label).last, + matching: find.byType(Material), + ); + final Material itemMaterial = tester.widget(buttonMaterial); + expect(itemMaterial.color, themeData.colorScheme.onSurface.withOpacity(0.12)); // Menu 1 button is highlighted. + }, variant: TargetPlatformVariant.desktop()); + testWidgets('The text input should match the label of the menu item ' 'while pressing down key on desktop platforms', (WidgetTester tester) async { final ThemeData themeData = ThemeData();