0

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

在此处输入图像描述

4

1 回答 1

1

将其包裹在 aContainerSizedBox宽度为,

new Container(
   width: 75.0,
   child: //your DropdownButton here
),

或者你可以Expanded在 a中使用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,
            ),
        ),
    ],
),

希望它可能会有所帮助。

于 2019-11-22T04:18:07.660 回答