我正在尝试创建弹出菜单。如何更改选定弹出菜单项的背景颜色?预先感谢!
代码示例:
PopupMenuButton(
icon: const Icon(
Icons.swap_vert,
size: 26,
color: Color(0xffffffff),
),
itemBuilder: (context) {
return [
PopupMenuItem(
value: 0,
child:
Text("Date",
style: GoogleFonts.poppins(
color: const Color(0xff000000),
fontSize: 14,
fontWeight: FontWeight.w500,
letterSpacing: 0.3,
),
PopupMenuItem<int>(
value: 1,
child: Text("Settings"),
),
PopupMenuItem<int>(
value: 2,
child: Text("Logout"),
),
];
},
onSelected: (value) {},
),
这是一个例子。用主题包裹 PopupMenuButton。
class PopupMenuExample extends StatefulWidget {
const PopupMenuExample({super.key});
@override
State<PopupMenuExample> createState() => _PopupMenuExampleState();
}
class _PopupMenuExampleState extends State<PopupMenuExample> {
int? selectedMenu;
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
highlightColor: Colors.lightBlueAccent,
),
child: PopupMenuButton<int>(
icon: const Icon(
Icons.swap_vert,
size: 26,
color: Color(0xffffffff),
),
initialValue: selectedMenu,
// Callback that sets the selected popup menu item.
onSelected: (int item) {
setState(() {
selectedMenu = item;
});
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<int>>[
const PopupMenuItem<int>(
value: 0,
child: Text('Example 1'),
),
const PopupMenuItem<int>(
value: 1,
child: Text('Example 2'),
),
const PopupMenuItem<int>(
value: 2,
child: Text('Example 3'),
),
],
),
);
}
}
使用主题
看这个:[1] [1]:https://%20https://blog.csdn.net/nicepainkiller/article/details/138909162?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A% 22文章%22%2C%22rId%22%3A%22138909162%22%2C%22来源%22%3A%22nicepainkiller%22%7D