简体   繁体   English

如何更改showMenu下PopupMenuItem的背景颜色?

[英]How to change the background color of PopupMenuItem under the showMenu?

I don't use PopupMenuButton. 我不使用PopupMenuButton。

Because in my case, showMenu would be more appropriate. 因为就我而言,showMenu会更合适。

onLongPress: () => _onRoomLongPressed(context, _tappedPosition, model, room),
void _onRoomLongPressed(BuildContext context, Offset tappedPosition,
    RoomsModel model, Room room) {
  final RenderBox overlay = Overlay.of(context).context.findRenderObject();

  showMenu(
    context: context,
    position: RelativeRect.fromRect(
        tappedPosition & Size.zero, Offset.zero & overlay.size),
    items: [
      PopupMenuItem(
        value: 'delete',
        child: Text('delete'),
      ),
    ],
  ).then((String value) {
    switch (value) {
      case 'delete':
        model.remove(room);
        break;
    }
  });
}

Don't change the theme of MaterialApp() . 不要更改MaterialApp()的主题。

I expect that the background color of PopupMenuItem to be white and the Text color black. 我希望PopupMenuItem的背景色是白色, Text色是黑色。

You need to wrap your widget that opens up the menu in a Builder and have Theme as the parent of this Builder . 您需要将可打开菜单的窗口小部件包装在Builder并将Theme作为此Builder的父级。 Here is the code that will get you going. 这是使您前进的代码。

@override
Widget build(BuildContext context) {
  return Theme(
    data: Theme.of(context).copyWith(
      cardColor: Colors.white,
      textTheme: Theme.of(context).textTheme.apply(bodyColor: Colors.black),
    ),
    child: Builder(
      builder: (context) {
        return RaisedButton(
          child: Text("Show menu"),
          onPressed: () {
            showMenu(
              context: context,
              position: RelativeRect.fromLTRB(0, 100, 0, 0),
              items: [
                PopupMenuItem(child: Text("Item 0"), value: 0),
                PopupMenuItem(child: Text("Item 1"), value: 1),
                PopupMenuItem(child: Text("Item 2"), value: 2),
              ],
            );
          },
        );
      },
    ),
  );
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM