如何在文本字段的dropdownbutton中添加标签文本?

时间:2019-08-01 21:26:00

标签: flutter

我可以在下拉按钮中添加标签文本吗?

这个代码库有可能吗?

String dropdownValue = 'One';
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: DropdownButton<String>(
        value: dropdownValue,
        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(),
      ),
    ),
  );
}

1 个答案:

答案 0 :(得分:0)

使用地图扩展您的列表以包含更多信息

  final myMapList = [
    {'Value': 'One', 'Label': 'item One'},
    {'Value': 'Two', 'Label': 'item Two'},
    {'Value': 'Three', 'Label': 'item Three'},
    {'Value': 'Four', 'Label': 'item Four'},
  ];

然后您可以在DropdownButton中使用它

        DropdownButton<String>(
          value: _newValue,
          onChanged: (String newValue) {
            setState(() {
              _newValue = newValue;
            });
          },
          items: myMapList.map((Map item) {
            return DropdownMenuItem<String>(
                value: item['Value'],
                child: Column(
                  children: <Widget>[
                    Text(item['Label']),
                    Text(item['Value']),
                  ],
                ));
          }).toList(),
        ),
相关问题