8000 DropdownMenu.didUpdateWidget should re-match initialSelection when dropdownMenuEntries have changed · Issue #155660 · flutter/flutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
DropdownMenu.didUpdateWidget should re-match initialSelection when dropdownMenuEntries have changed #155660
@pamtbaau

Description

@pamtbaau

Use case

In my use case, the DropdownMenu.dropdownMenuEntries are asynchronously fetched from the database and assigned after the Future has been resolved. The initialSelection, however, has already been set in the first build cycle.

The problem lays in that in DropdownMenu.didUpdateWidget(), where the initialSelection is not matched again against the updated menu entries. See

if (oldWidget.dropdownMenuEntries != widget.dropdownMenuEntries) {
currentHighlight = null;
filteredEntries = widget.dropdownMenuEntries;
buttonItemKeys = List<GlobalKey>.generate(filteredEntries.length, (int index) => GlobalKey());
_menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry<T> entry) => entry.enabled);
}

This will lead to an empty field not showing the matching initialSelection, but yet showing a correct list of items in the dropdown. Quite confusing...

Finding out the cause after debugging, I use the following, hopefully temporary, workaround:

DropdownMenu<String>(
  initialSelection: items.isEmpty ? null : _selectedItem ?? widget.item,
  ... etc
)

Proposal

I would like the initialSelection to be matched again against the new value set for dropdownMenuEntries as is done when the intialSelection has changed. See

if (oldWidget.initialSelection != widget.initialSelection) {
final int index = filteredEntries.indexWhere((DropdownMenuEntry<T> entry) => entry.value == widget.initialSelection);
if (index != -1) {
_localTextEditingController?.value = TextEditingValue(
text: filteredEntries[index].label,
selection: TextSelection.collapsed(offset: filteredEntries[index].label.length),
);
}
}

IMHO this should be provided by Flutter directly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterf: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages teamworkaround availableThere is a workaround available to overcome the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0