dart 如何更改Flutter中打开的下拉菜单中所选项目的背景颜色?

uurv41yg  于 11个月前  发布在  Flutter
关注(0)|答案(2)|浏览(143)

如何在打开的下拉菜单中更改所选项目的背景颜色?我想在打开的下拉菜单中更改所选项目的背景颜色。


的数据

ruyhziif

ruyhziif1#

我可以通过添加MenuStyle来更改颜色

DropdownMenu(
  menuStyle: MenuStyle(
    backgroundColor:  MaterialStateProperty.all( Colors.grey),
  ),
),

字符串

lhcgjxsq

lhcgjxsq2#

在Flutter中,您可以通过自定义项目并使用DropdownMenuItem或自定义背景颜色来更改打开的DropdownButton中所选项目的背景颜色。以下是您的操作方法:
1.创建DropdownMenuItem或小部件列表并自定义其外观:

DropdownButton<String>(
         value: selectedValue,
         items: items.map((String item) {
             return DropdownMenuItem<String>(
                 value: item,
                 child: Container(
                     color: item == selectedValue ? Colors.blue : Colors.white, // Change the background color for the selected item
                     child: Text(item),
                 ),
             );
         }).toList(),
         onChanged: (String newValue) {
             setState(() {
                 selectedValue = newValue;
             });
         },
     )

字符串
在上面的代码中,Container的背景颜色是通过比较当前项目和选定的值来确定的。如果匹配,它将背景颜色设置为蓝色(您可以将其更改为您喜欢的任何颜色),对于其他项目,它将背景颜色设置为白色。您可以根据自己的喜好自定义颜色。

  • 确保您有一个selectedValue变量,并在onChanged回调中更新它以反映所选的值。

相关问题