Reputation: 4565
How to set dynamically no of PopupMenuItem
to the PopupMenuButton
with index like ListView.builder()
.
Actually i'm using FutureBuilder
to get data from the api and in response , 4 items are fetched means it gives dynamic no of items.
and want to show them into PopupMenuItem
in list form .
but there is no count constructor in PopupMenuButton
unlike ListView.builder()
.
Code:
FutureBuilder<CommonModel>(
future: futureListMaterialStatusCommonModel,
builder:(BuildContext context, AsyncSnapshot<CommonModel> snapshot ){
if(!snapshot.hasData){
return Text("loading...");
}
if(snapshot.hasError){
return Text(snapshot.error);
}
//I tried to get solution by loop before showing items but went wrong
for(int i ; i< snapshot.data.content.length; i++){
itemList.add(
PopupMenuItem(
value: snapshot.data.content[i].title,//919876543210,
child: Text(
snapshot.data.content[0].title,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700),
),
),
);
}
return PopupMenuButton<String>(
itemBuilder: (context) => [
PopupMenuItem(
value: snapshot.data.content[0].title,
child: Text(
snapshot.data.content[0].title,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700),
),
),
],
onSelected: (value) {
setState(() {
materialStatus = value;
});
},
child: materialStatus==null? Text("Select...",
style: TextStyle(color:Colors.orange),)
:Text(materialStatus,
style: TextStyle(color:Colors.black, fontWeight: FontWeight.bold),)
);
} ,
)
Upvotes: 0
Views: 4080
Reputation: 4565
Finally I made it. it was so simple, really so dumb I'm.
return PopupMenuButton<String>(
itemBuilder: (context ) =>
snapshot.data.content
.map((item) => PopupMenuItem<String>(
value: item.title,
child: Text(
item.title,
),
))
.toList(),
onSelected: (value) {
setState(() {
materialStatus = value;
});
},
child: materialStatus ==null? Text("Select...",
style: TextStyle(color:Colors.orange),)
:Text(materialStatus,
style: TextStyle(color:Colors.black, fontWeight: FontWeight.bold),)
);
Upvotes: 6