如何为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,
),
])
答案 0 :(得分:1)
将其包装在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,
),
),
],
),
希望这会有所帮助。