簡體   English   中英

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

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

我不使用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色是黑色。

您需要將可打開菜單的窗口小部件包裝在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),
              ],
            );
          },
        );
      },
    ),
  );
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM