Skip to content

DropdownMenuEntry.labelWidget is not used for the selected item #166999

@matanlurey

Description

@matanlurey

It appears (to me) that DropdownMenuEntry.labelWidget is ignored when rendering the selected item.

What that means in practice is if I have a complex object (i.e. without a reasonable toString()), and I want to rely entirely on a provided itemBuilder, the label appears to take precedence over labelWidget.

For example, in the following custom wrapper widget, Branch is shown instead of itemBuilder(option):

Image

final class DropdownSelect<T> extends StatelessWidget {
  const DropdownSelect({
    required this.options,
    required this.itemBuilder,
    required this.selected,
    this.onSelect,
  });

  /// Options that can be selected.
  final List<T> options;

  /// Returns a [Widget] representing the item for [T].
  ///
  /// ## Example
  ///
  /// ```dart
  /// TappableSelect(
  ///   itemBuilder: (item) => Text(item.name),
  /// )
  /// ```
  final Widget Function(T) itemBuilder;

  /// Currently selected element of type [T].
  final T selected;

  /// Invoked when an item is selected.
  ///
  /// If `null` this select is read only.
  final void Function(T)? onSelect;

  @override
  Widget build(BuildContext context) {
    return DropdownMenu(
      hintText: 'Select a branch',
      dropdownMenuEntries: [
        ...options.map((option) {
          return DropdownMenuEntry(
            value: option,
            label: '',
            labelWidget: itemBuilder(option),
          );
        }),
      ],
      enableFilter: true,
      initialSelection: selected,
      onSelected: invokeIfNonNull(onSelect),
      label: Text('Branch'),
    );
  }
}

Is that WAI?

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.r: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions