Repository navigation
Implement DropdownButton.selectedItemBuilder - #40461
Conversation
| items = List<Widget>.from(widget.items); | ||
| else if (_enabled && widget.selectedItemBuilder != null) | ||
| items = widget.selectedItemBuilder(context).map((Widget item) { | ||
| return Container( |
There was a problem hiding this comment.
I opted to wrap the items in a similar Container as DropdownMenuItem instead of using DropdownMenuItem itself since DropdownMenuItem takes a value that determines whether it is selected or not, and that may not make sense here
| // item and the width of the hint. | ||
| final List<Widget> items = _enabled ? List<Widget>.from(widget.items) : <Widget>[]; | ||
| List<Widget> items = <Widget>[]; | ||
| if (_enabled && widget.selectedItemBuilder == null) |
There was a problem hiding this comment.
maybe
if (_enabled) {
items = List<Widget>.from(widget.items);
} else {
...
}}
| /// corresponds to but is unique from [DropdownMenuItem]. | ||
| /// | ||
| /// ```dart | ||
| /// final List<String> items = <String>[ |
There was a problem hiding this comment.
A slightly more realistic example might be to have the selected items just be '1', '2', '3'. Sometimes apps need to keep the dropdown buttons horizontally compact, even though the menu items are not.
| /// {@endtemplate} | ||
| final ValueChanged<T> onChanged; | ||
|
|
||
| /// A builder to generate custom dropdown buttons corresponding to the |
There was a problem hiding this comment.
You might reconsider the terminology here. The dropdown button includes the down-arrow icon. So we're not generating the button, although we're certainly customizing it.
* Implement DropdownButton.selectedItemBuilder
Description
This PR allows
DropdownButtonto have custom selected dropdown buttons. This is particularly useful if developers need to display a selected item button that is unique from the dropdown menu items.Once
DropdownButton's multi-line sizing/height issue has been addressed, this change can be used to properly control the overflow in text for the dropdown button and its items. For example, the button can setText('text', maxLines: 1)while the menu item can setText('text', maxLines 2).Related Issues
#9211 - Helps decouple the button's contents from the menu's contents if necessary
Tests
I added the following tests:
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Does your PR require Flutter developers to manually update their apps to accommodate your change?