How to change the background color of Popup MenuButton bullet window?
Asked Answered
E

7

10

I want to change the background color of Popup Menu Button window. What should I do? I hope I can get your help. Thank you.When I change the color of container, some corners cannot change the color.

 new IconButton(
            icon: new Icon(
              Icons.search,
              color: Colors.white,
            ),
            onPressed: () {},
          ),
          new PopupMenuButton(
            offset: const Offset(0.0, 60.0),
            icon: new Icon(Icons.add, color: Colors.white),
            itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                  new PopupMenuItem<String>(
                      value: '选项一的值',
                      child: new Container(
                          color: Colors.red,
                          child: new Column(
                            children: <Widget>[
                              new Row(
                                children: <Widget>[
                                  new Image.asset(defaultAvatar,
                                      width: 30.0, height: 30.0),
                                  new Text('发起群聊')
                                ],
                              ),
                            ],
                          ))),
                  new PopupMenuItem<String>(
                      value: '选项一的值',
                      child: new Container(
                          child: new Column(
                        children: <Widget>[
                          new Row(
                            children: <Widget>[
                              new Image.asset(defaultAvatar,
                                  width: 30.0, height: 30.0),
                              new Text('添加朋友')
                            ],
                          ),
                        ],
                      ))),

enter image description here

Eats answered 7/12, 2018 at 1:55 Comment(0)
S
13

That background color is based on the Theme , so you can change the color wrapping your PopMenuButton inside Theme and change the cardColor.

          Theme(
                data: Theme.of(context).copyWith(
                  cardColor: Colors.red,
                ),
                child: new PopupMenuButton(
                       ...
Serpigo answered 7/12, 2018 at 5:22 Comment(2)
cardColor: has not worked for me , so i did it with canvasColor:Neapolitan
I needed to change the active item's highlight color, so in my case, I used highlightColor.Unlicensed
H
8

It's kinda ugly, but hey:

PopupMenuButton<String>(
   onSelected: (selected) {},
   icon: Icon(Icons.more_vert, color: Colors.white,),
   itemBuilder: (BuildContext context) {
      ...
   },
), 
Hunker answered 13/11, 2019 at 12:44 Comment(0)
S
5

By default PopupMenuButton has a tint color.

surfaceTintColor = popupMenuTheme.shadowColor

so we also need to set its color, to make it work.

PopupMenuButton(
  color: Colors.red,
  surfaceTintColor: Colors.red,
  child: ...
),
Sanious answered 26/9, 2023 at 8:50 Comment(0)
A
2

You can just change the background color using PopupMenuButton( color: Colors.red, ...) without having to wrap it in a new theme.

Anaerobic answered 4/8, 2020 at 12:44 Comment(0)
K
1
Center(
      child: Theme(
        data: Theme.of(context).copyWith(
          cardColor: Colors.red,
        ),
        child: PopupMenuButton(
          child: Text("Show Popup Menu"),
          itemBuilder: (context) => [
                PopupMenuItem(
                  child: Text("InduceSmile.com"),
                ),
                PopupMenuItem(
                  child: Text("Flutter.io"),
                ),
                PopupMenuItem(
                  child: Text("Google.com"),
                ),
              ],
        ),
      ),

This is working fine.

Karlakarlan answered 22/10, 2020 at 20:38 Comment(0)
H
1

Starting from Flutter 3.10, it works like this:

PopupMenuButton(
      color: Colors.red,
      child: ...
),
Hardboiled answered 7/7, 2023 at 22:39 Comment(0)
J
1
{Color? color}
Type: Color?

If provided, the background color used for the menu.

If this property is null, then [PopupMenuThemeData.color] is used. If [PopupMenuThemeData.color] is also null, then Theme.of(context).cardColor is used.

PopupMenuButton(
  color: Colors.red,
  child: ...
),
Jorin answered 17/3 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.