Skip to content

Commit 1c27fa6

Browse files
Add menuController to DropdownMenu (flutter#175039)
Fixes flutter#174780 ## 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]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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 27d8897 commit 1c27fa6

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class DropdownMenu<T> extends StatefulWidget {
197197
this.textInputAction,
198198
this.cursorHeight,
199199
this.restorationId,
200+
this.menuController,
200201
}) : assert(filterCallback == null || enableFilter),
201202
assert(
202203
inputDecorationTheme == null ||
@@ -595,6 +596,10 @@ class DropdownMenu<T> extends StatefulWidget {
595596
/// {@macro flutter.material.textfield.restorationId}
596597
final String? restorationId;
597598

599+
/// An optional controller that allows opening and closing of the menu from
600+
/// other widgets.
601+
final MenuController? menuController;
602+
598603
@override
599604
State<DropdownMenu<T>> createState() => _DropdownMenuState<T>();
600605
}
@@ -603,7 +608,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
603608
final GlobalKey _anchorKey = GlobalKey();
604609
final GlobalKey _leadingKey = GlobalKey();
605610
late List<GlobalKey> buttonItemKeys;
606-
final MenuController _controller = MenuController();
611+
late MenuController _controller;
607612
bool _enableFilter = false;
608613
late bool _enableSearch;
609614
late List<DropdownMenuEntry<T>> filteredEntries;
@@ -642,6 +647,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
642647
_selectedEntryIndex = index;
643648
}
644649
refreshLeadingPadding();
650+
_controller = widget.menuController ?? MenuController();
645651
}
646652

647653
@override
@@ -712,6 +718,9 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
712718
_selectedEntryIndex = index;
713719
}
714720
}
721+
if (oldWidget.menuController != widget.menuController) {
722+
_controller = widget.menuController ?? MenuController();
723+
}
715724
}
716725

717726
bool canRequestFocus() {

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,6 +4740,27 @@ void main() {
47404740
final EditableText editableText = tester.widget(find.byType(EditableText));
47414741
expect(editableText.cursorHeight, cursorHeight);
47424742
});
4743+
4744+
testWidgets('DropdownMenu accepts a MenuController', (WidgetTester tester) async {
4745+
final MenuController menuController = MenuController();
4746+
await tester.pumpWidget(
4747+
MaterialApp(
4748+
home: Scaffold(
4749+
body: DropdownMenu<TestMenu>(
4750+
menuController: menuController,
4751+
dropdownMenuEntries: menuChildren,
4752+
),
4753+
),
4754+
),
4755+
);
4756+
expect(findMenuItemButton('Item 0').hitTestable(), findsNothing);
4757+
menuController.open();
4758+
await tester.pumpAndSettle();
4759+
expect(findMenuItemButton('Item 0').hitTestable(), findsOne);
4760+
menuController.close();
4761+
await tester.pumpAndSettle();
4762+
expect(findMenuItemButton('Item 0').hitTestable(), findsNothing);
4763+
});
47434764
}
47444765

47454766
enum TestMenu {

0 commit comments

Comments
 (0)