如何在下拉菜单中突出显示选中的项目?目前,当下拉菜单打开时,已经选中的菜单项是无法理解的。
可以在DropdownButton
中使用selectedItemBuilder
参数:
DropdownButton<String>(
value: dropdownValue,
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
style: const TextStyle(color: Colors.blue),
selectedItemBuilder: (BuildContext context) {
return options.map((String value) {
return Text(
dropdownValue,
style: const TextStyle(color: Colors.white),
);
}).toList();
},
items: options.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
还可以使用items
参数更改不同选项的样式,如上面的示例所示。