简体   繁体   English

如何循环Flutter中PopupMenuButton中的PopupMenuItem?

[英]How to loop the PopupMenuItem in PopupMenuButton in Flutter?

I want to display values from my API in a PopupMenuItem in PopupMenuButton.我想在 PopupMenuButton 的 PopupMenuItem 中显示我的 API 中的值。 I manage to display it but I want it to be dynamic.我设法显示它,但我希望它是动态的。 Currently, I hard-coded the index of each item because it seems that I cannot do looping inside PopupMenuButton.目前,我对每个项目的索引进行了硬编码,因为我似乎无法在 PopupMenuButton 中进行循环。

`Widget _simplePopup4() => PopupMenuButton<int>(
    child:  Icon(Icons.arrow_drop_down, color: Colors.orangeAccent),
      offset: Offset(0, 100),
    itemBuilder: (context) => [

      PopupMenuItem(
        value: 1,
        child: Container(
            child: FutureBuilder<SettingCtrl>(
                future: getSettingCtrl(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (snapshot.data.setTitle == null) {
                      return Container();
                    } else {
                      return Text(snapshot.data.setTitle[1].title); //index 1
                    }
                  }
                  return CircularProgressIndicator();
                })),
      ),
      PopupMenuDivider(),
      PopupMenuItem(
        value: 1,
        child: Container(
            child: FutureBuilder<SettingCtrl>(
                future: getSettingCtrl(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (snapshot.data.setTitle == null) {
                      return Container();
                    } else {
                      return Text(snapshot.data.setTitle[2].title); //index 2
                    }
                  }
                  return CircularProgressIndicator();
                })),
      ),
      PopupMenuDivider(),
      PopupMenuItem(
        value: 1,
        child: Container(
            child: FutureBuilder<SettingCtrl>(
                future: getSettingCtrl(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (snapshot.data.setTitle == null) {
                      return Container();
                    } else {
                      return Text(snapshot.data.setTitle[3].title); //index 3
                    }
                  }
                  return CircularProgressIndicator();
                })),
      ),
    ],
  );`

//First attempt which gives error: RenderShrinkWrappingViewport does not support returning intrinsic dimensions. // 给出错误的第一次尝试:RenderShrinkWrappingViewport 不支持返回固有尺寸。

      Widget _simplePopup5() => PopupMenuButton(
    itemBuilder: (context) {
      var list = List<PopupMenuEntry<Object>>();
      list.add(
        PopupMenuItem(
          value: 1,
          child:  Container(
              child: FutureBuilder<SettingCtrl>(
             
                  future: getSettingCtrl(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      if (snapshot.data.setTitle == null) {
                        return Container();
                      } else {
                        return ListView.builder(
                                physics: NeverScrollableScrollPhysics(),
                                scrollDirection: Axis.vertical,
                                shrinkWrap: true,
                                itemCount: snapshot.data.setTitle.length,
                                itemBuilder:
                                    (BuildContext context, int index) {
                                  return Text(snapshot.data.setTitle[index].title);
                                });
                      }
                    }
                    return CircularProgressIndicator();
                  })),
        ),
      );
      list.add(
        PopupMenuDivider(
          height: 10,
        ),
      );
      return list;
    },
    icon: Icon(
      Icons.settings,
      size: 50,
      color: Colors.white,
    ),
  );

//Second attempt which gives error: Another exception was thrown: A RenderFlex overflowed by 85 pixels on the bottom. //第二次尝试给出错误:抛出另一个异常:RenderFlex 在底部溢出 85 像素。

      Widget _simplePopup5() => PopupMenuButton(
    itemBuilder: (context) {
      var list = List<PopupMenuEntry<Object>>();
      list.add(
        PopupMenuItem(
          value: 1,
          child: Container(
              child: FutureBuilder<SettingCtrl>(
                  future: getSettingCtrl(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      final listTitle = <Widget>[];
                      for (var i = 0;
                      i < snapshot.data.setTitle.length;
                      i++) {
                        listTitle.add(SingleChildScrollView(
                            scrollDirection: Axis.vertical,
                            child: InkWell(
                              child:
                              Text(snapshot.data.setTitle[i].title),
                            )));
                      }

                      if (snapshot.data.setTitle == null) {
                        return Container();
                      } else {
                        return Column(children: listTitle);
                      }
                    }
                    return CircularProgressIndicator();
                  })),
        ),
      );
      list.add(
        PopupMenuDivider(
          height: 10,
        ),
      );
      return list;
    },
    icon: Icon(
      Icons.settings,
      size: 50,
      color: Colors.white,
    ),
  );

From the screenshot, only one item are clearly displayed which is "MR" while the other item (before item "MR") are displayed in half.从屏幕截图中,只有一个项目清楚地显示为“MR”,而另一个项目(在项目“MR”之前)则显示为一半。 Meanwhile, the rest of the item (after item "MR") being replaced with error message.同时,项目的 rest(在项目“MR”之后)被替换为错误消息。

The screenshot of the second attempt error第二次尝试错误的截图

第二次尝试错误的截图

The cause of the RenderFlex error is because the child Widget expands beyond the parent Widget. RenderFlex 错误的原因是因为子 Widget 扩展到父 Widget 之外。 What you can do here is fetch the List of PopupMenu items prior to rendering the PopupMenuButton .您在这里可以做的是在呈现PopupMenuButton之前获取 PopupMenu 项目列表。 With this approach, the List items is ready prior to clicking the PopupMenuButton .使用这种方法,列表项在单击PopupMenuButton之前准备就绪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM