Flutter DropdownButton在选择选项时显示标签

时间:2019-06-01 20:13:24

标签: flutter

可以Dropdown Button

 return DropdownButton<String>(
          items: <String>['Foo', 'Bar'].map((String value) {
            return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
            );
          }).toList(),
          onChanged: (_) {},
        );

TextFormField中的装饰用户类似:

      TextFormField(
        controller: _titleController,
        decoration: InputDecoration(labelText: 'Input'),
        validator: (String value) {
          if (value != null && value.isEmpty) {
            return 'Please enter some text';
          }
        },
        style: Theme.of(context).textTheme.title,
      ),

当在上面的TextFormField中写入内容时,将显示单词Input。像这样:

enter image description here

2 个答案:

答案 0 :(得分:5)

用DropdownButtonFormField替换DropdownButton:

https://api.flutter.dev/flutter/material/DropdownButtonFormField-class.html

答案 1 :(得分:1)

将DropdownButton更改为DropdownButtonFormField并添加此装饰...。

              decoration: InputDecoration(
                filled: true,
                fillColor: Hexcolor('#ecedec'),
                labelText: 'Occupation',
                border: new CustomBorderTextFieldSkin().getSkin(),
              ),