Skip to content

Commit 896cd91

Browse files
authored
Fix DropdownMenu keyboard navigation on filtered entries (flutter#165868)
Fix `DropdownMenu` keyboard navigation when `enableFilter` is set to true. Nothing major here, just a simple bugfix. ## Related Issues - Fixes flutter#165867 ## Tests Added 1 test. ## 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. - [x] 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
1 parent cd0082f commit 896cd91

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
884884
currentHighlight = null;
885885
controller.close();
886886
} else {
887+
filteredEntries = widget.dropdownMenuEntries;
887888
// close to open
888889
if (_localTextEditingController!.text.isNotEmpty) {
889890
_enableFilter = false;
@@ -934,8 +935,6 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
934935
filteredEntries =
935936
widget.filterCallback?.call(filteredEntries, _localTextEditingController!.text) ??
936937
filter(widget.dropdownMenuEntries, _localTextEditingController!);
937-
} else {
938-
filteredEntries = widget.dropdownMenuEntries;
939938
}
940939
_menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry<T> entry) => entry.enabled);
941940

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,55 @@ void main() {
17011701
expect(tester.takeException(), isNull);
17021702
});
17031703

1704+
// Regression test for https://github.com/flutter/flutter/issues/165867.
1705+
testWidgets('Keyboard navigation only traverses filtered entries', (WidgetTester tester) async {
1706+
final TextEditingController controller = TextEditingController();
1707+
addTearDown(controller.dispose);
1708+
1709+
await tester.pumpWidget(
1710+
MaterialApp(
1711+
home: Scaffold(
1712+
body: DropdownMenu<TestMenu>(
1713+
requestFocusOnTap: true,
1714+
enableFilter: true,
1715+
controller: controller,
1716+
dropdownMenuEntries: const <DropdownMenuEntry<TestMenu>>[
1717+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu0, label: 'Good Match 1'),
1718+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu1, label: 'Bad Match 1'),
1719+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu2, label: 'Good Match 2'),
1720+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu3, label: 'Bad Match 2'),
1721+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu4, label: 'Good Match 3'),
1722+
DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu5, label: 'Bad Match 3'),
1723+
],
1724+
),
1725+
),
1726+
),
1727+
);
1728+
1729+
// Open the menu.
1730+
await tester.tap(find.byType(DropdownMenu<TestMenu>));
1731+
await tester.pump();
1732+
1733+
// Filter the entries to only show the ones with 'Good Match'.
1734+
await tester.enterText(find.byType(TextField), 'Good Match');
1735+
await tester.pump();
1736+
1737+
// Since the first entry is already highlighted, navigate to the second item.
1738+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
1739+
await tester.pump();
1740+
expect(controller.text, 'Good Match 2');
1741+
1742+
// Navigate to the third item.
1743+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
1744+
await tester.pump();
1745+
expect(controller.text, 'Good Match 3');
1746+
1747+
// Navigate back to the first item.
1748+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
1749+
await tester.pump();
1750+
expect(controller.text, 'Good Match 1');
1751+
});
1752+
17041753
// Regression test for https://github.com/flutter/flutter/issues/147253.
17051754
testWidgets('Default search prioritises the current highlight', (WidgetTester tester) async {
17061755
final ThemeData themeData = ThemeData();

0 commit comments

Comments
 (0)