Flutter PopupMenuButton from a list

101 Views Asked by At

In Flutter, I need to create a PopupMenu dynamically

I would like to make something like

List<String> items = ['item 1','item2','item3];

PopupMenuButton(
  itemBuilder: (context) => items;
  onSelected: return items.value;
),

instead of

PopupMenuButton(
  itemBuilder: (context) => [
    PopupMenuItem(
      child: Text(item1),
      value: item1
    ),
    PopupMenuItem(
      child: Text(item1),
      value: item1
    ),
    PopupMenuItem(
      child: Text(item1),
      value: item1
    )
  ],
  onSelected: (string newValue {},
)

somebody could kindly help me ?

1

There are 1 best solutions below

1
On

You can map the items.

PopupMenuButton(
  itemBuilder: (context) => items.map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
  onSelected: (value) {
    print(value); //use a state variable 
  },
);