如何解决Flutter下拉按钮溢出问题?

时间:2018-11-08 17:11:10

标签: dart flutter overflow dropdown

我创建一个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...';
                          },
                        ),
                      ],
                    ))));

2 个答案:

答案 0 :(得分:4)

尽管我已将问题标记为possible duplicate,但另一个问题中未提及的部分解决方案是将isExpanded属性用于DropDownButton

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

答案 1 :(得分:3)

在大多数情况下,除了扩展外,最好将其椭圆化。步骤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) { }
)
相关问题