如何为Flutter中的DropDownButton提供恒定的宽度?
据我所知,为什么下图中的DropDownButton都具有不同的宽度大小,作为第一个值最大的长度“ 100%”第二个值的最大值为“ 90%”,因此字符串的长度有所不同,因此相应地进行了包装。
我想给它们一个固定的宽度,以使它们看起来相同。
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(title,
style: Theme.textTheme.body1
.copyWith(fontWeight: ThemeData.medium)),
DropdownButton(
disabledHint: Text(defaultValue),
value: currentDropDownItem,
items: dropDownItemList,
onChanged: isEnabled
? (value) {
if (onValueChanged != null) {
onValueChanged(value);
}
}
: null,
),
])
将其包装在Container
或SizedBox
中,宽度为,
new Container(
width: 75.0,
child: //your DropdownButton here
),
或者您可以在Expanded
中使用Row
,
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 5,
child: Text(
title,
style: Theme.textTheme.body1
.copyWith(fontWeight: ThemeData.medium),
),
),
Expanded(
flex: 1,
child: DropdownButton(
disabledHint: Text(defaultValue),
value: currentDropDownItem,
items: dropDownItemList,
onChanged: isEnabled
? (value) {
if (onValueChanged != null) {
onValueChanged(value);
}
}
: null,
),
),
],
),
希望可能会有帮助。