How to dynamically update the menu content of the PopupMenuButton (flutter)?
I'm using flutter PopUpMenuButton from material library and I'm trying to To dynamically change the content of the PopUpMenuButton based on the loading state of mylist.
E.g : I have Drawer class that holds the myItems list and the loading flag. The initState method is called when the widget is created, and I load list asynchronously there. When the list is loaded, setState is called to update the state.
In the build method, the PopupMenuButton is constructed. If the loading flag is true, it shows a loading indicator. Once the list is loaded, it maps the myItems list to PopupMenuItem widgets.
PopupMenuButton(
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
child: Builder(
builder: (context) {
if (loading) {
return Text('Loading...');
} else {
return Column(
children: myItems.map((String item) {
return PopupMenuItem(
child: Text(item),
value: item,
);
}).toList(),
);
}
},
),
),
];
},
onSelected: (value) {
print(value);
},
);
When I click on the button, the menu opens with the loading indicator. In the mean time, the list is loaded, but the problem is that content of the menu is not updated (it still showing loading indicator) until I close the menu and open it again.
I tried with StatefulBuilder but still is not updating...
Can anyone knows the solution?