问题
In Flutter, I can build a Dropdown with DropdownMenuItems, like this:
The DropdownMenuItems I add are always wider than the dropdown itself:
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,
),
),
);
回答1:
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,
),
),
),
);
回答2:
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,
),
),
);
来源:https://stackoverflow.com/questions/48895195/how-can-we-change-the-width-padding-of-a-flutter-dropdownmenuitem-in-a-dropdown