繁体   English   中英

Flutter DropdownButton 在选择选项时显示 label

[英]Flutter DropdownButton show label when option is selected

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

在此处输入图像描述

用 DropdownButtonFormField 替换 DropdownButton:

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

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

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

复制粘贴,看看魔法
我用材料设计完成了 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(),
              ),
            ),
          ),
        ),
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM