Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent selectedItemBuilder from moving text up

Using a DropdownButton, I need the selected item to be a different color than the list of items in the dropdown menu that appears when you tap the button. According to the Flutter documentation, I should use selectedItemBuilder. As can be seen right there in the example within the documentation, using the selectedItemBuilder results with the selected text being shifted up which does not look good.

selected item in dropdown shifted up

How can I get the selected item to be down in line with the drop-down icon where it is supposed to be while having separate colors for the menu items and the selected item?

like image 783
Stack Underflow Avatar asked Jan 24 '26 06:01

Stack Underflow


1 Answers

I found that this is an open issue on GitHub. A work around is to wrap the widget returned by the builder (and also the hint if you have one) in a Center widget.

DropdownButton<String>(
  selectedItemBuilder: (_) {
    return items.map<Widget>((String item) {
      return Center(
        child: Text(item),
      );
    }).toList();
  },
  hint: Center(child: myHintWidget),

  ...

like image 188
Stack Underflow Avatar answered Jan 25 '26 23:01

Stack Underflow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!