From 91cb2618c3c2731b4f958726f43ba6d14500527a Mon Sep 17 00:00:00 2001
From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com>
Date: Thu, 29 Jun 2023 18:48:05 -0700
Subject: [PATCH] Make `DropdownMenu` be able to scroll to the highlighted item
when searching. (#129740)
Fixes #120349
This PR enables `DropdownMenu` to automatically scroll to the first matching item when `enableSearch` is true.
video example
https://github.com/flutter/flutter/assets/36861262/1a7a956c-c186-44ca-9a52-d94dc21cac8a
---
.../lib/src/material/dropdown_menu.dart | 29 +++++++++++++++++--
.../test/material/dropdown_menu_test.dart | 23 +++++++++++++++
2 files changed, 49 insertions(+), 3 deletions(-)
diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart
index 802fdfe37c..0d7125d6be 100644
--- a/packages/flutter/lib/src/material/dropdown_menu.dart
+++ b/packages/flutter/lib/src/material/dropdown_menu.dart
@@ -284,6 +284,7 @@ class DropdownMenu extends StatefulWidget {
class _DropdownMenuState extends State> {
final GlobalKey _anchorKey = GlobalKey();
final GlobalKey _leadingKey = GlobalKey();
+ late List buttonItemKeys;
final MenuController _controller = MenuController();
late final TextEditingController _textEditingController;
late bool _enableFilter;
@@ -299,6 +300,7 @@ class _DropdownMenuState extends State> {
_textEditingController = widget.controller ?? TextEditingController();
_enableFilter = widget.enableFilter;
filteredEntries = widget.dropdownMenuEntries;
+ buttonItemKeys = List.generate(filteredEntries.length, (int index) => GlobalKey());
_menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry entry) => entry.enabled);
final int index = filteredEntries.indexWhere((DropdownMenuEntry entry) => entry.value == widget.initialSelection);
@@ -313,7 +315,15 @@ class _DropdownMenuState extends State> {
@override
void didUpdateWidget(DropdownMenu oldWidget) {
super.didUpdateWidget(oldWidget);
+ if (oldWidget.enableSearch != widget.enableSearch) {
+ if (!widget.enableSearch) {
+ currentHighlight = null;
+ }
+ }
if (oldWidget.dropdownMenuEntries != widget.dropdownMenuEntries) {
+ currentHighlight = null;
+ filteredEntries = widget.dropdownMenuEntries;
+ buttonItemKeys = List.generate(filteredEntries.length, (int index) => GlobalKey());
_menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry entry) => entry.enabled);
}
if (oldWidget.leadingIcon != widget.leadingIcon) {
@@ -354,11 +364,20 @@ class _DropdownMenuState extends State> {
});
}
+ void scrollToHighlight() {
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ final BuildContext? highlightContext = buttonItemKeys[currentHighlight!].currentContext;
+ if (highlightContext != null) {
+ Scrollable.ensureVisible(highlightContext);
+ }
+ });
+ }
+
double? getWidth(GlobalKey key) {
final BuildContext? context = key.currentContext;
if (context != null) {
final RenderBox box = context.findRenderObject()! as RenderBox;
- return box.size.width;
+ return box.hasSize ? box.size.width : null;
}
return null;
}
@@ -384,7 +403,7 @@ class _DropdownMenuState extends State> {
List> filteredEntries,
TextEditingController textEditingController,
TextDirection textDirection,
- { int? focusedIndex }
+ { int? focusedIndex, bool enableScrollToHighlight = true}
) {
final List result = [];
final double padding = leadingPadding ?? _kDefaultHorizontalPadding;
@@ -416,6 +435,7 @@ class _DropdownMenuState extends State> {
: effectiveStyle;
final MenuItemButton menuItemButton = MenuItemButton(
+ key: enableScrollToHighlight ? buttonItemKeys[i] : null,
style: effectiveStyle,
leadingIcon: entry.leadingIcon,
trailingIcon: entry.trailingIcon,
@@ -490,7 +510,7 @@ class _DropdownMenuState extends State> {
@override
Widget build(BuildContext context) {
final TextDirection textDirection = Directionality.of(context);
- _initialMenu ??= _buildButtons(widget.dropdownMenuEntries, _textEditingController, textDirection);
+ _initialMenu ??= _buildButtons(widget.dropdownMenuEntries, _textEditingController, textDirection, enableScrollToHighlight: false);
final DropdownMenuThemeData theme = DropdownMenuTheme.of(context);
final DropdownMenuThemeData defaults = _DropdownMenuDefaultsM3(context);
@@ -500,6 +520,9 @@ class _DropdownMenuState extends State> {
if (widget.enableSearch) {
currentHighlight = search(filteredEntries, _textEditingController);
+ if (currentHighlight != null) {
+ scrollToHighlight();
+ }
}
final List menu = _buildButtons(filteredEntries, _textEditingController, textDirection, focusedIndex: currentHighlight);
diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart
index 50fba56e13..71f05e87ae 100644
--- a/packages/flutter/test/material/dropdown_menu_test.dart
+++ b/packages/flutter/test/material/dropdown_menu_test.dart
@@ -1315,6 +1315,29 @@ void main() {
await tester.pumpWidget(buildFrame());
expect(find.text(errorText), findsOneWidget);
});
+
+ testWidgets('Can scroll to the highlighted item', (WidgetTester tester) async {
+ await tester.pumpWidget(MaterialApp(
+ home: Scaffold(
+ body: DropdownMenu(
+ requestFocusOnTap: true,
+ menuHeight: 100, // Give a small number so the list can only show 2 or 3 items.
+ dropdownMenuEntries: menuChildren,
+ ),
+ ),
+ ));
+ await tester.pumpAndSettle();
+
+ await tester.tap(find.byType(DropdownMenu));
+ await tester.pumpAndSettle();
+
+ expect(find.text('Item 5').hitTestable(), findsNothing);
+ await tester.enterText(find.byType(TextField), '5');
+ await tester.pumpAndSettle();
+ // Item 5 should show up.
+ expect(find.text('Item 5').hitTestable(), findsOneWidget);
+ });
+
}
enum TestMenu {