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

2eafrhcq  于 2023-05-08  发布在  Flutter
关注(0)|答案(3)|浏览(204)

可以一个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。就像这样:

z9smfwbn

z9smfwbn2#

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

DropdownButtonFormField<String>(
  decoration: InputDecoration(
    filled: true,
    fillColor: Hexcolor('#ecedec'),
    labelText: 'Occupation',
    border: new CustomBorderTextFieldSkin().getSkin(),
  ),
  //...
)
mzaanser

mzaanser3#

复制粘贴看魔术

我已经做了Flutter下拉与材料设计

Padding(
          padding: const EdgeInsets.all(9.0),
          child: InputDecorator(
            decoration: InputDecoration(
              labelText: 'Priority',
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5.0)),
              contentPadding: EdgeInsets.all(10),
            ),
            child: ButtonTheme(
              materialTapTargetSize: MaterialTapTargetSize.padded,
              child: DropdownButton<String>(
                hint: const Text("Priority"),
                isExpanded: true,
                value: dropdownValue,
                elevation: 16,
                underline: DropdownButtonHideUnderline(
                  child: Container(),
                ),
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownValue = newValue!;
                  });
                },
                items: <String>['One', 'Two', 'Free', 'Four']
                    .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
          ),
        ),

相关问题