如何为Flutter DropdownButton小部件提供恒定的宽度?

时间:2019-11-22 04:11:32

标签: flutter widget flutter-layout dropdownbutton

如何为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,
      ),
    ])

enter image description here

1 个答案:

答案 0 :(得分:1)

将其包装在ContainerSizedBox内,宽度为

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,
            ),
        ),
    ],
),

希望这会有所帮助。