如何在flutter中为弹出菜单的每个项目着色?

nafvub8i  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(105)

我想给每个容器给予颜色。我试着把它 Package 在一个容器里,然后分配颜色,但它没有给整个物品容器着色。我试过所有可能的方法,但它不起作用。enter image description here

PopupMenuButton<String>(
  itemBuilder: (BuildContext context) {
    return <PopupMenuEntry<String>>[
      PopupMenuItem<String>(
        value: 'option1',
        child: ListTile(
          leading: Icon(Icons.delete),
          title: Text('Delete'),
        ),
      ),
      PopupMenuDivider(),
      PopupMenuItem<String>(
        value: 'option2',
        child: ListTile(
          leading: Icon(Icons.edit),
          title: Text('Edit'),
        ),
      ),
      PopupMenuItem<String>(
        value: 'option3',
        child: ListTile(
          leading: Icon(Icons.share),
          title: Text('Share'),
        ),
      ),
    ];
  },
  onSelected: (String value) {
    // Handle the selected option
    switch (value) {
      case 'option1':
        // Perform delete operation
        break;
      case 'option2':
        // Perform edit operation
        break;
      case 'option3':
        // Perform share operation
        break;
    }
  },
)

];

字符串

1aaf6o9v

1aaf6o9v1#

实现此目的的一种方法是使用Container post Package 每个PopupMenuItem的子对象,您可以设置该Containercolor属性。类似于:

PopupMenuButton<String>(
  itemBuilder: (BuildContext context) {
    return <PopupMenuEntry<String>>[
      PopupMenuItem<String>(
        value: 'option1',
        child: Container( // <---- Notice this
          color: Colors.red, // Set the color here as per your need
          child: ListTile(
            leading: Icon(Icons.delete),
            title: Text('Delete'),
          ),
        ),
      ),
      // Similarly for others...

字符串

相关问题