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.
This commit is contained in:
PurplePolyhedron
2024-08-22 01:00:15 +08:00
committed by GitHub
parent f0a37e70e5
commit e13e9a08e1
2 changed files with 115 additions and 1 deletions

View File

@@ -489,6 +489,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
late List<GlobalKey> buttonItemKeys;
final MenuController _controller = MenuController();
bool _enableFilter = false;
late bool _enableSearch;
late List<DropdownMenuEntry<T>> filteredEntries;
List<Widget>? _initialMenu;
int? currentHighlight;
@@ -504,6 +505,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
} else {
_localTextEditingController = TextEditingController();
}
_enableSearch = widget.enableSearch;
filteredEntries = widget.dropdownMenuEntries;
buttonItemKeys = List<GlobalKey>.generate(filteredEntries.length, (int index) => GlobalKey());
_menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry<T> entry) => entry.enabled);
@@ -543,6 +545,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
}
if (oldWidget.enableSearch != widget.enableSearch) {
if (!widget.enableSearch) {
_enableSearch = widget.enableSearch;
currentHighlight = null;
}
}
@@ -698,6 +701,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
return;
}
_enableFilter = false;
_enableSearch = false;
currentHighlight ??= 0;
currentHighlight = (currentHighlight! - 1) % filteredEntries.length;
while (!filteredEntries[currentHighlight!].enabled) {
@@ -717,6 +721,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
return;
}
_enableFilter = false;
_enableSearch = false;
currentHighlight ??= -1;
currentHighlight = (currentHighlight! + 1) % filteredEntries.length;
while (!filteredEntries[currentHighlight!].enabled) {
@@ -757,7 +762,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
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<T> extends State<DropdownMenu<T>> {
setState(() {
filteredEntries = widget.dropdownMenuEntries;
_enableFilter = widget.enableFilter;
_enableSearch = widget.enableSearch;
});
},
inputFormatters: widget.inputFormatters,

View File

@@ -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<int>(
searchCallback: (List<DropdownMenuEntry<int>> entries, String query) {
if (query.isEmpty) {
return null;
}
final int index = entries.indexWhere(
(DropdownMenuEntry<int> entry) => entry.label.contains(query),
);
return index != -1 ? index : null;
},
dropdownMenuEntries: const <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(
value: 0,
label: 'ABC'
),
DropdownMenuEntry<int>(
value: 1,
label: 'AB'
),
DropdownMenuEntry<int>(
value: 2,
label: 'ABCD'
),
],
),
),
));
await tester.tap(find.byType(DropdownMenu<int>));
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<Material>(button0Material);
expect(item0Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
Material item1Material = tester.widget<Material>(button1Material);
expect(item1Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
final Material item2Material = tester.widget<Material>(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<Material>(button1Material);
expect(item1Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.pump();
item0Material = tester.widget<Material>(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<TestMenu>(
dropdownMenuEntries: menuChildren,
),
),
));
// Open the menu and highlight the first item.
await tester.tap(find.byType(DropdownMenu<TestMenu>));
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<Material>(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();