dart 如何改变所选弹出菜单项的背景颜色?

wbrvyc0a  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在尝试创建弹出菜单。我怎样才能改变一个选定的弹出菜单项的背景颜色?提前感谢!
像这样:x1c 0d1x
代码示例:

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) {},
                ),
vktxenjb

vktxenjb1#

这里有一个例子。用主题 Package 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'),
          ),
        ],
      ),
    );
  }

}

相关问题