Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we change the width/padding of a Flutter DropdownMenuItem in a Dropdown?

In Flutter, I can build a Dropdown with DropdownMenuItems, like this: enter image description here

The DropdownMenuItems I add are always wider than the dropdown itself:

enter image description here

How do you adjust the width of the DropdownMenuItem, or remove the extra horizontal padding?

My DropdownMenuItem widget looks like this:

DropdownMenuItem(     value: unit.name,     child: Text('hey'), ); 

while my Dropdown widget looks like this:

return Container(     width: 300.0,     child: DropdownButtonHideUnderline(       child: DropdownButton(         value: name,         items: listOfDropdownMenuItems,         onChanged: onChanged,         style: Theme.of(context).textTheme.title,       ),     ), ); 
like image 218
Mary Avatar asked Feb 20 '18 22:02

Mary


People also ask

How do you change the width of a dropdown in flutter?

Setting the Width for a Dropdown Button You can set the width for a DropdownButton by setting its isExpanded parameter to true and wrapping it inside a fixed-size Container or SIzedBox.

How do you change the height and width of a dropdown button in flutter?

You just need to leave the itemHeight with the null value. It will make the height of the DropdownButton with the menu item's intrinsic height.


2 Answers

This feature has been added. See https://github.com/flutter/flutter/pull/14849

You can now wrap it in a ButtonTheme and set alignedDropdown to true.

return Container(     width: 300.0,     child: DropdownButtonHideUnderline(       child: ButtonTheme(         alignedDropdown: true,           child: DropdownButton(             value: name,             items: listOfDropdownMenuItems,             onChanged: onChanged,             style: Theme.of(context).textTheme.title,          ),       ),     ), ); 
like image 178
Mary Avatar answered Sep 24 '22 04:09

Mary


I solved this problem with changing isExpanded to true;

return Container(     width: 300.0,     child: DropdownButtonHideUnderline(       child: DropdownButton(         isExpanded: true,         value: name,         items: listOfDropdownMenuItems,         onChanged: onChanged,         style: Theme.of(context).textTheme.title,       ),     ), ); 
like image 26
Mehmet Ali Bayram Avatar answered Sep 22 '22 04:09

Mehmet Ali Bayram