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

问题描述 投票:0回答:1

如何为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

flutter widget flutter-layout dropdownbutton
1个回答
0
投票

将其包装在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,
            ),
        ),
    ],
),

希望可能会有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.