DropdownButtonFormField,具有固定宽度但动态文本项

DropdownButtonFormField, with fixed width but dynamic text items

我认为我不太了解 Flutter 中的约束,所以请耐心等待!

我想要 DropdownButtonFormField 从数据库填充其项目。该字符串可以是任何动态长度。所以我决定的是固定宽度 DropdownButtonFormField 并且 DropdownMenuItem 将省略 Text.

这是我试过的。

SizedBox(
  width: 136.0,
  child: DropdownButtonFormField<int>(
    hint: Text("hintText")
    decoration: InputDecoration(
        contentPadding: const EdgeInsets.all(0.0),
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.white),
        ),
        isDense: true),
    items: [
        DropdownMenuItem<int>(
            value: 0,
            child: TextOneLine(
              "less character",
            ),
        ),
        DropdownMenuItem<int>(
            value: 0,
            child: TextOneLine(
              "mooooorrrrreeee character",
            ),
        )
    ]
  ),
);

class TextOneLine extends StatelessWidget {
  final String data;
  final TextStyle style;
  final TextAlign textAlign;
  final bool autoSize;

  TextOneLine(
    String data, {
    Key key,
    this.style,
    this.textAlign,
    this.autoSize = false,
  })  : this.data = data,
        assert(data != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(
        data,
        style: style,
        textAlign: textAlign,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      );
  }
}

如何消除溢出错误?我不能使用 Flexible 或 Expanded DropDownButtonFormField,因为字符串长度可能是动态的(可能比适合的长度更长)。

请参考所附图片,我添加了 3 张图片。

图 1:这就是您遇到的问题。

图 2:当我从 SizedBox 中删除 width 时。现在它显示 3 个框,第一个是提示文本,另一个是空的,第三个是下拉箭头。我认为溢出是由第二个空 space 引起的。

图 3:现在,我再次为 136SizedBox 添加了一个宽度,并将 SizedBox 放在具有固定宽度的 Container100 的大小(是下拉列表中文本的宽度,它肯定会根据宽度包装您的文本)。根据您提供的代码,这解决了溢出问题。

我认为是因为您添加了一个 TextOneLine 导致问题的自定义小部件。可能还有一些其他解决方法,但这解决了问题。

SizedBox(
        width: 136,
        child: DropdownButtonFormField<int>(
            hint: Text("hintText"),
            decoration: InputDecoration(
                contentPadding: const EdgeInsets.all(0.0),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.white),
                ),
                isDense: true),
            items: [
              DropdownMenuItem<int>(
                value: 0,
                child: Container(
                  width: 100,
                  child: TextOneLine(
                    "less character",
                  ),
                ),
              ),
              DropdownMenuItem<int>(
                  value: 0,
                  child: Container(
                    width: 100,
                    child: TextOneLine(
                      "mooooorrrrreeee character",
                    ),
                  ))
            ]),
      )

试试这个,让我知道这是否是问题所在(并已解决),请让我们了解您所做的任何其他解决方法的最新情况。谢谢

那是因为您的列表中有一个项目包含太多字符 像 "mooooorrrrreeee character" 或类似的

我知道现在分享答案可能有点晚,但我找到了一个简单的解决方法。只需将 isExpanded: true 添加到 DropdownButtonFormField。

  DropdownButtonFormField<int>(
        hint: Text("hintText"),
        isExpanded: true,
        decoration: InputDecoration(
            contentPadding: const EdgeInsets.all(0.0),
            enabledBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white),
            ),
            isDense: true),
        items: [
          DropdownMenuItem<int>(
            value: 0,
            child: Container(
              width: 100,
              child: TextOneLine(
                "less character",
              ),
            ),
          ),
          DropdownMenuItem<int>(
              value: 0,
              child: Container(
                width: 100,
                child: TextOneLine(
                  "mooooorrrrreeee character",
                ),
              ))
        ])

Before Adding isExpanded property After Adding isExpanded property