salim-lachdhaf / dropdown_search

Simple and robust Dropdown with item search feature, making it possible to use an offline item list or filtering URL for easy customization.
MIT License
346 stars 334 forks source link

Probably `DropdownSearchBuilder` could have non nullable `selectedItem` #706

Open vasilich6107 opened 2 weeks ago

vasilich6107 commented 2 weeks ago

Is your feature request related to a problem? Please describe.

Here is an fn typedef

typedef DropdownSearchBuilder<T> = Widget Function(BuildContext context, T? selectedItem);

Selected item being defined as nullable at the same time input decorator states

isEmpty: getSelectedItem == null,

Describe the solution you'd like This means that the content will not be shown, considering this - selectedItem for DropdownSearchBuilder could be non nullable cause there is no chance it will be shown.

Describe alternatives you've considered

dropdownBuilder: (_, item) {
        if (item == null) {
          return const SizedBox.shrink();
        }
        return Row(
          children: [
            ...item
          ],
        );
      },
salim-lachdhaf commented 1 week ago

DropdownSearchBuilder should be a nullable selectedItem otherwise, it will be impossible to represent "nothing" value .

isEmpty only affects the display of the hint or label in the dropdown.

Behavior:

Examples:

  1. If the developer handles null as follows, the hint remains inside the text field without issue:

    dropdownBuilder: (_, item) {
     if (item == null) {
       return const SizedBox.shrink();
     }
     return Row(
       children: [
         ...item
       ],
     );
    },
  2. If the developer uses a non-empty or visible widget for a null item, like this:

    dropdownBuilder: (_, item) {
     if (item == null) {
       return Text('No item selected');
     }
     return Row(
       children: [
         ...item
       ],
     );
    },

    In this case, the hint is hidden by default, and the developer should use floatingLabelBehavior to explicitly show the hint as a label.

For more context, you can refer to this related topic: GitHub issue #515.