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

时间:2019-04-02 14:10:18

标签: dart flutter

我不使用PopupMenuButton。

因为在我的情况下,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;
    }
  });
}

请勿更改MaterialApp()的主题。

我希望PopupMenuItem的背景颜色为白色,Text的颜色为黑色。

1 个答案:

答案 0 :(得分:0)

您需要将打开菜单的小部件包装在Builder中,并以Theme作为此Builder的父级。这是使您前进的代码。

@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),
              ],
            );
          },
        );
      },
    ),
  );
}

enter image description here