如何在下拉列表中更改选定选项的颜色?
以下是下拉列表的代码,其中所选文本和菜单中的所有文本均具有相同的颜色,即黑色。选择文本时,我想要白色的颜色,同时,就像菜单项是黑色的,以便在下拉列表的白色背景上可见。
child: DropdownButtonFormField(
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.cyan, width: 1.0),
borderRadius: BorderRadius.circular(10),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white, width: 1.0),
borderRadius: BorderRadius.circular(10),
),
hintText: 'Year of Education',
hintStyle: TextStyle(
color: Color.fromARGB(255, 245, 244, 255),
fontStyle: FontStyle.italic,
fontSize: 10,
),
),
value: dropdownValue,
items: const [
DropdownMenuItem<String>(
child: Text('-choose-'), value: ''),
DropdownMenuItem<String>(
child: Text('First'), value: 'First'),
DropdownMenuItem<String>(
child: Text('Second'), value: 'Second'),
DropdownMenuItem<String>(
child: Text('Third'), value: 'Third'),
DropdownMenuItem<String>(
child: Text('Fourth'), value: 'Fourth'),
DropdownMenuItem<String>(
child: Text('Fifth'), value: 'Fifth'),
],
onChanged: (String? value) {
setState(() {
dropdownValue = value!;
TextStyle(color: Colors.white);
});
},
validator: (value) {
if (dropdownValue == '')
return 'You must select a value.';
return null;
}),
Below is the code for a dropdown list where the selected text and all the texts in the menu are of the same color, i.e., black. I want a white color for the text when it is selected and, simultaneously, like the menu items to be black so that they are visible over the white background of the dropdown list.
child: DropdownButtonFormField(
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.cyan, width: 1.0),
borderRadius: BorderRadius.circular(10),
),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.white, width: 1.0),
borderRadius: BorderRadius.circular(10),
),
hintText: 'Year of Education',
hintStyle: TextStyle(
color: Color.fromARGB(255, 245, 244, 255),
fontStyle: FontStyle.italic,
fontSize: 10,
),
),
value: dropdownValue,
items: const [
DropdownMenuItem<String>(
child: Text('-choose-'), value: ''),
DropdownMenuItem<String>(
child: Text('First'), value: 'First'),
DropdownMenuItem<String>(
child: Text('Second'), value: 'Second'),
DropdownMenuItem<String>(
child: Text('Third'), value: 'Third'),
DropdownMenuItem<String>(
child: Text('Fourth'), value: 'Fourth'),
DropdownMenuItem<String>(
child: Text('Fifth'), value: 'Fifth'),
],
onChanged: (String? value) {
setState(() {
dropdownValue = value!;
TextStyle(color: Colors.white);
});
},
validator: (value) {
if (dropdownValue == '')
return 'You must select a value.';
return null;
}),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种方法可以做到这一点。
如果项目是动态的,并且小部件数据是从此数组列表中构建的,则更简单。
当小部件以类似以下条件构建项目时,这将在列表上迭代:
其中
myitemSarray
是您的动态阵列;但是,如果您坚持在小部件内构建列表数据,则必须按以下方式复制每个项目的条件:
当然,您可以随时更改样式。
There are two approaches for doing this.
If the items are dynamic and the widget data is built from this array list, then it's simpler.
This will iterate over the list when the widget builds the item with a condition like the following:
Where
myItemsArray
is your dynamic array;But, if you insist on building the list data inside the widget, then you must duplicate the condition for each item as follows:
Of course you can change the style however you wish.
您可以包括
样式
将在所选项目上使用。另外,要更改列表,请在项目上删除
const
并关注You can include
style
that will be used on the selected item.Also, to change on list, remove
const
on items and follow