【问题标题】:How to give a constant width to a Flutter DropdownButton widget?如何为 Flutter DropdownButton 小部件提供恒定宽度?
【发布时间】:2020-03-18 02:54:52
【问题描述】:

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

【问题讨论】:

    标签: flutter widget flutter-layout dropdownbutton


    【解决方案1】:

    将其包裹在 ContainerSizedBox 中,宽度为,

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

    或者您可以在Row 中使用Expanded

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2019-07-16
      • 2021-10-30
      • 2019-12-23
      • 1970-01-01
      • 2021-11-05
      • 2012-09-05
      相关资源
      最近更新 更多