如何动态地给弹出式菜单按钮加上flutter的弹出式菜单项数?

问题描述 投票:0回答:1

如何动态设置弹出菜单项的数量?PopupMenuItemPopupMenuButton 指数 ListView.builder().

其实我用的是 FutureBuilder 从api获取数据,在响应中,获取4个项目,这意味着它给出了动态的项目数,并希望将它们显示到。PopupMenuItem 列表形式,但在列表中没有count构造函数。PopupMenuButton 不像 ListView.builder().

编码。

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),)
                  );
                } ,
              )
flutter button dart popup
1个回答
0
投票

最后我终于做出来了.太简单了,真的太笨了,我。

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),)
                 );
© www.soinside.com 2019 - 2024. All rights reserved.