diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 51b17e5b2c..0b74b397d1 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -21,6 +21,13 @@ import 'text_field.dart'; import 'theme.dart'; import 'theme_data.dart'; +/// A callback function that returns the index of the item that matches the +/// current contents of a text field. +/// +/// If a match doesn't exist then null must be returned. +/// +/// Used by [DropdownMenu.searchCallback]. +typedef SearchCallback = int? Function(List> entries, String query); // Navigation shortcuts to move the selected menu items up or down. Map _kMenuTraversalShortcuts = { @@ -150,6 +157,7 @@ class DropdownMenu extends StatefulWidget { this.onSelected, this.requestFocusOnTap, this.expandedInsets, + this.searchCallback, required this.dropdownMenuEntries, }); @@ -303,6 +311,34 @@ class DropdownMenu extends StatefulWidget { /// Defaults to null. final EdgeInsets? expandedInsets; + /// When [DropdownMenu.enableSearch] is true, this callback is used to compute + /// the index of the search result to be highlighted. + /// + /// {@tool snippet} + /// + /// In this example the `searchCallback` returns the index of the search result + /// that exactly matches the query. + /// + /// ```dart + /// DropdownMenu( + /// searchCallback: (List> entries, String query) { + /// if (query.isEmpty) { + /// return null; + /// } + /// final int index = entries.indexWhere((DropdownMenuEntry entry) => entry.label == query); + /// + /// return index != -1 ? index : null; + /// }, + /// dropdownMenuEntries: const >[], + /// ) + /// ``` + /// {@end-tool} + /// + /// Defaults to null. If this is null and [DropdownMenu.enableSearch] is true, + /// the default function will return the index of the first matching result + /// which contains the contents of the text input field. + final SearchCallback? searchCallback; + @override State> createState() => _DropdownMenuState(); } @@ -564,7 +600,11 @@ class _DropdownMenuState extends State> { } if (widget.enableSearch) { - currentHighlight = search(filteredEntries, _textEditingController); + if (widget.searchCallback != null) { + currentHighlight = widget.searchCallback!.call(filteredEntries, _textEditingController.text); + } else { + currentHighlight = search(filteredEntries, _textEditingController); + } if (currentHighlight != null) { scrollToHighlight(); } diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 3cbd44428d..27b0e9dafc 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -1726,6 +1726,85 @@ void main() { // so there are some extra padding before "Item 1". expect(tester.getTopLeft(find.text('Item 1').last).dx, 48.0); }); + + testWidgetsWithLeakTracking('DropdownMenu can have customized search algorithm', (WidgetTester tester) async { + final ThemeData theme = ThemeData(); + Widget dropdownMenu({ SearchCallback? searchCallback }) { + return MaterialApp( + theme: theme, + home: Scaffold( + body: DropdownMenu( + requestFocusOnTap: true, + searchCallback: searchCallback, + dropdownMenuEntries: const >[ + DropdownMenuEntry(value: 0, label: 'All'), + DropdownMenuEntry(value: 1, label: 'Unread'), + DropdownMenuEntry(value: 2, label: 'Read'), + ], + ), + ) + ); + } + + void checkExpectedHighlight({String? searchResult, required List otherItems}) { + if (searchResult != null) { + final Finder material = find.descendant( + of: find.widgetWithText(MenuItemButton, searchResult).last, + matching: find.byType(Material), + ); + final Material itemMaterial = tester.widget(material); + expect(itemMaterial.color, theme.colorScheme.onSurface.withOpacity(0.12)); + } + + for (final String nonHighlight in otherItems) { + final Finder material = find.descendant( + of: find.widgetWithText(MenuItemButton, nonHighlight).last, + matching: find.byType(Material), + ); + final Material itemMaterial = tester.widget(material); + expect(itemMaterial.color, Colors.transparent); + } + } + + // Test default. + await tester.pumpWidget(dropdownMenu()); + await tester.pump(); + await tester.tap(find.byType(DropdownMenu)); + await tester.pumpAndSettle(); + + await tester.enterText(find.byType(TextField), 'read'); + await tester.pump(); + checkExpectedHighlight(searchResult: 'Unread', otherItems: ['All', 'Read']); // Because "Unread" contains "read". + + // Test custom search algorithm. + await tester.pumpWidget(dropdownMenu( + searchCallback: (_, __) => 0 + )); + await tester.pump(); + await tester.enterText(find.byType(TextField), 'read'); + await tester.pump(); + checkExpectedHighlight(searchResult: 'All', otherItems: ['Unread', 'Read']); // Because the search result should always be index 0. + + // Test custom search algorithm - exact match. + await tester.pumpWidget(dropdownMenu( + searchCallback: (List> entries, String query) { + if (query.isEmpty) { + return null; + } + final int index = entries.indexWhere((DropdownMenuEntry entry) => entry.label == query); + + return index != -1 ? index : null; + }, + )); + await tester.pump(); + + await tester.enterText(find.byType(TextField), 'read'); + await tester.pump(); + checkExpectedHighlight(otherItems: ['All', 'Unread', 'Read']); // Because it's case sensitive. + await tester.enterText(find.byType(TextField), 'Read'); + await tester.pump(); + checkExpectedHighlight(searchResult: 'Read', otherItems: ['All', 'Unread']); + }); } enum TestMenu {