如何解决Flutter下拉菜单溢出问题?

11

我创建了一个 Flutter 表单,用 Flutter 构建了一个下拉按钮。我将本地数据放入下拉列表中,但是有些选项很长。我使用 SafeArea 和 ListView,但是右侧出现了溢出问题。

在其他问题中没有提到的部分解决方案,我在这里找到了答案。

有任何想法如何解决这个问题吗?

 // TODO: BUILD RUN
        return new Scaffold(
            key: _scaffoldKey,
            body: new SafeArea(
                top: false,
                bottom: false,
                child: new Form(
                    key: _formKey,
                    child: new ListView(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16.0, vertical: 32.0),
                      children: <Widget>[
                        //TODO: CURRENCY 
                        new FormField<String>(
                          builder: (FormFieldState<String> state) {
                            return InputDecorator(
                              decoration: InputDecoration(
                                labelText: 'CHOOSE CURRENCY',
                                labelStyle: TextStyle(
                                    fontSize: 18.0,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.green.shade700),
                                errorText: state.hasError ? state.errorText : null,
                              ),
                              isEmpty: _mySelectedCurrency == '',
                              child: new DropdownButtonHideUnderline(
                                child: new DropdownButton<String>(
                                  style: TextStyle(
                                    fontSize: 14.0,
                                    color: Colors.black,
                                    fontWeight: FontWeight.w500,
                                  ),
                                  value: _mySelectedCurrency,
                                  isDense: true,
                                  onChanged: (String newValue) {
                                    setState(() {
                                      _mySelectedCurrency = newValue;
                                      state.didChange(newValue);
                                    });
                                  },
                                  items: _itemsName,
                                ),
                              ),
                            );
                          },
                          validator: (val) {
                            return val != '' ? null : 'Choose Currency...';
                          },
                        ),
                      ],
                    ))));

可能是Flutter - ListView中的DropdownButton溢出的重复问题。 - chemamolins
是的,它很相似...而且似乎GitHub问题还没有解决。问题在于解决方法对我来说行不通,下拉菜单项有时会超过3行...正在寻找固定和关闭的解决方案,谢谢。 - Nick
2个回答

53

虽然我已将问题标记为可能重复,但未在其他问题中提到的部分解决方案是使用DropDownButtonisExpanded属性。

              child: new DropdownButton<String>(
                isExpanded: true,
                ...

谢谢,它可以正常工作而没有任何错误或溢出。但我需要问一下,你说的“部分解决方案”是什么意思? - Nick
1
阅读Github问题中的最后一条评论后,我不得不同意发帖人的看法。它可以工作,但可能不适用于所有情况。 - chemamolins
谢谢,我还在寻找最终的解决方案,我修改了我的菜单项,所以目前没有问题... - Nick
那确实帮了我很多!谢谢。 - Bliv_Dev
同样适用于DropdownButtonFormField. - Shobi

9
在大多数情况下,除了扩展之外,将其缩略也是很好的选择...步骤1和2。如果不缩略,它会在下一行换行,如果组件不支持多行,则会截断文本。
DropdownButton(
    isExpanded: true, //Step 1
    items: [
        new DropdownMenuItem(
            child: Text("Long text that overflow the size.. wrapped or ellipsized", 
            overflow: TextOverflow.ellipsis),  //Step 2
        ),
    ],
    onChanged: (val) { }
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接