Make DropdownButton's disabledHint and hint behavior consistent#42479
Make DropdownButton's disabledHint and hint behavior consistent#42479shihaohong merged 9 commits intoflutter:masterfrom shihaohong:dropdown-disabled-hint
Conversation
| } | ||
|
|
||
| if (widget.selectedItemBuilder == null) { | ||
| displayedHint = Container( |
There was a problem hiding this comment.
This Container mimics the constraints and alignments for a default menu item, similar to DropdownMenuItem, when selectedItemBuilder is not being used
There was a problem hiding this comment.
It would probably be safest to either include a comment here and in DropdownMenuItem.build() indicating that the code must be kept in sync, or add a _DropdownMenuItemContainer widget and use it in both places. The widget approach might be preferable, since you can explain what's going on vis-a-vis selectedItemBuilder in a comment above the widget class.
There was a problem hiding this comment.
Sounds good, I just added a _DropdownMenuItemContainer and had DropdownMenuItem extend it to keep the code in sync. PTAL
| final Widget emplacedHint = _enabled | ||
| ? widget.hint | ||
| : DropdownMenuItem<Widget>(child: widget.disabledHint ?? widget.hint); | ||
| Widget displayedHint; |
There was a problem hiding this comment.
It doesn't really matter but this could be:
Widget displayedHint = _enabled ? widget.hint : widget.disabledHint ?? widget.hint;Also: thank you for eliminating the name "emplacedHint" :-).
| } | ||
|
|
||
| if (widget.selectedItemBuilder == null) { | ||
| displayedHint = Container( |
There was a problem hiding this comment.
It would probably be safest to either include a comment here and in DropdownMenuItem.build() indicating that the code must be kept in sync, or add a _DropdownMenuItemContainer widget and use it in both places. The widget approach might be preferable, since you can explain what's going on vis-a-vis selectedItemBuilder in a comment above the widget class.
| ); | ||
| }).toList(), | ||
| selectedItemBuilder: selectedItemBuilder, | ||
| itemHeight: itemHeight, |
There was a problem hiding this comment.
Good: tests that don't specify itemHeight should work as before since kMinInteractiveDimension is the default.
| int hintIndex; | ||
| if (widget.hint != null || (!_enabled && widget.disabledHint != null)) { | ||
| final Widget emplacedHint = _enabled | ||
| ? widget.hint |
There was a problem hiding this comment.
Previously, an app that specified a hint for an enabled DropdownButton, would not see that hint aligned and its minHeight would not be constrained (because we didn't wrap it). That seems like a pointless inconsistency, but we'll need to watch out for problems introduced by this.
There was a problem hiding this comment.
Right -- this would be the breaking change part of this PR, I think it might still be worth sending an email out to Flutter developers to notify them of the correct fix if they run into problems here
| disabledHint: null, | ||
| ); | ||
| } | ||
| // [hint] should display when [items] is null and [disabledHint] is not defined |
There was a problem hiding this comment.
According to https://api.flutter.dev/flutter/material/DropdownButton/hint.html, the hint is displayed if value is null.
There seems to be some pre-existing confusion about this. See https://api.flutter.dev/flutter/material/DropdownButton/value.html. Can you make the docs consistent about when the hint is displayed, and what exactly makes the button enabled/disabled?
There was a problem hiding this comment.
I've updated the docs to be consistent with what exactly makes a button enabled/disabled.
HansMuller
left a comment
There was a problem hiding this comment.
LGTM.
This is a now a twofer!
| /// The value of the currently selected [DropdownMenuItem]. | ||
| /// | ||
| /// If [value] is null and [hint] is non-null, the [hint] widget is | ||
| /// displayed as a placeholder for the dropdown button. |
There was a problem hiding this comment.
... for the dropdown button's value.
| /// A placeholder widget that is displayed by the dropdown button. | ||
| /// | ||
| /// If [value] is null, this widget is displayed as a placeholder for | ||
| /// the dropdown button. This widget is also displayed if the button |
There was a problem hiding this comment.
dropdown button => dropdown button's value
| /// displayed in grey and it will not respond to input. A disabled button | ||
| /// will display the [disabledHint] widget if it is non-null. | ||
| /// will display the [disabledHint] widget if it is non-null. If | ||
| /// [disabledHint] is also null but [hint] is non-null, [hint] will instead |
There was a problem hiding this comment.
will instead be displayed => will be displayed instead
…ter#42479) * Fix DropdownButton disabledHint behavior * Fix hint behavior when selectedItemBuilder is null * Improve variable names, some formatting updates * Create _DropdownMenuItemContainer widget * Improve API docs to be consistent with hint/disabledHint actual behavior
Description
This PR brings consistency to DropdownButton's hint behavior in the following ways:
DropdownButton.selectedItemBuilder = null, it now correctly wraps the hint with a Container with the correct constraints and alignment (To match the defaults ofDropdownMenuItem).DropdownButton.selectedItemBuilderis defined, hint and disabledHint are no longer wrapped with the constrained Container, allowing for more flexibility in creating the hint and disabledHint widgets.Related Issues
Fixes #42472
Fixes #43040
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?