Skip to content

Implement DropdownButton.selectedItemBuilder - #40461

Merged
shihaohong merged 5 commits into
flutter:masterfrom
shihaohong:dropdown-multiline
Sep 14, 2019
Merged

shihaohong merged 5 commits into
flutter:masterfrom
shihaohong:dropdown-multiline

Conversation

@shihaohong

Copy link
Copy Markdown
Contributor

Description

This PR allows DropdownButton to 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 set Text('text', maxLines: 1) while the menu item can set Text('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:

  • A test to verify that the custom items are properly displayed

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.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@shihaohong shihaohong added c: new feature Nothing broken; request for a new capability framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages labels Sep 13, 2019
items = List<Widget>.from(widget.items);
else if (_enabled && widget.selectedItemBuilder != null)
items = widget.selectedItemBuilder(context).map((Widget item) {
return Container(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@HansMuller HansMuller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

if (_enabled) {
  items = List<Widget>.from(widget.items);
} else {
 ...
}

}

/// corresponds to but is unique from [DropdownMenuItem].
///
/// ```dart
/// final List<String> items = <String>[

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@shihaohong
shihaohong merged commit cf7e7e4 into flutter:master Sep 14, 2019
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Sep 30, 2019
* Implement DropdownButton.selectedItemBuilder
@shihaohong
shihaohong deleted the dropdown-multiline branch November 7, 2019 19:00
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

c: new feature Nothing broken; request for a new capability framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants