如何在 flutter 中创建带有动态数据的 PopUpMenu?

发布于 2025-01-08 23:50:14 字数 1416 浏览 3 评论 0原文

我想用动态数据显示 PopupMenu,这些数据是我从 API 响应中获得的。我尝试在 PopupMenu 子项中使用 listview.builder 但它不起作用。

输入图片这里的描述

我的 showmenu() 代码

void showMemberMenu() async {
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(200, 150, 100, 100),
      items: [
        PopupMenuItem(
          value: 1,
          child: Text(
            "ROHIT",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
        PopupMenuItem(
          value: 2,
          child: Text(
            "REKHA",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
        PopupMenuItem(
          value: 3,
          child: Text(
            "DHRUV",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
      ],
      elevation: 8.0,
    ).then((value) {
      if (value != null) print(value);
    });
  }

请帮助摆脱这个问题。

I want to show PopupMenu with dynamic data which will I got from API response. I tried with listview.builder inside PopupMenu child but it not works.

enter image description here

My code of showmenu()

void showMemberMenu() async {
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(200, 150, 100, 100),
      items: [
        PopupMenuItem(
          value: 1,
          child: Text(
            "ROHIT",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
        PopupMenuItem(
          value: 2,
          child: Text(
            "REKHA",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
        PopupMenuItem(
          value: 3,
          child: Text(
            "DHRUV",
            style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.bold,
                fontFamily: 'Roboto',
                color: green3),
          ),
        ),
      ],
      elevation: 8.0,
    ).then((value) {
      if (value != null) print(value);
    });
  }

Please help to get out from this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2025-01-15 23:50:15

您可以使用 List.generate 方法使用从 API 响应中获取的现有列表来生成列表的动态长度。

下面是如何实现它的示例。

  void showMemberMenu() async {
    final List<String> popList = ['ROHIT', 'REKHA', 'DHRUV'];
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(200, 150, 100, 100),
      items: List.generate(
        popList.length,
        (index) => PopupMenuItem(
          value: 1,
          child: Text(
            popList[index],
            style: TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.bold,
              fontFamily: 'Roboto',
            ),
          ),
        ),
      ),
      elevation: 8.0,
    ).then((value) {
      if (value != null) print(value);
    });
  }

我添加了final List; popList = ['ROHIT', 'REKHA', 'DHRUV'];
仅用于测试目的,您可以将其替换为您的列表
您从 API 响应中获得。

You can use List.generate method to generate the dynamic length of the list using the existing list you get from the API response.

Below is an example of how you can achieve it.

  void showMemberMenu() async {
    final List<String> popList = ['ROHIT', 'REKHA', 'DHRUV'];
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(200, 150, 100, 100),
      items: List.generate(
        popList.length,
        (index) => PopupMenuItem(
          value: 1,
          child: Text(
            popList[index],
            style: TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.bold,
              fontFamily: 'Roboto',
            ),
          ),
        ),
      ),
      elevation: 8.0,
    ).then((value) {
      if (value != null) print(value);
    });
  }

I've added final List<String> popList = ['ROHIT', 'REKHA', 'DHRUV'];
just for the testing purpose, and you can replace it with your list
you get from API response.

舂唻埖巳落 2025-01-15 23:50:15

试试这个:

调用api并将值放入PopupMenuItem

class PopupMenu {
 PopupMenu({@required this.title, @required this.onTap});

 final String title;
 final VoidCallback onTap;

 static PopupMenuButton<String> createPopup(List<PopupMenu> popupItems) {
 return PopupMenuButton<String>(
  onSelected: (value) {
    popupItems.firstWhere((e) => e.title == value).onTap();
  },
  itemBuilder: (context) => popupItems
      .map((item) => PopupMenuItem<String>(
            value: item.title,
            child: Text(
              item.title,
            ),
          ))
      .toList(),
    );
   }
 }

Try this:

call the api and put the value in PopupMenuItem

class PopupMenu {
 PopupMenu({@required this.title, @required this.onTap});

 final String title;
 final VoidCallback onTap;

 static PopupMenuButton<String> createPopup(List<PopupMenu> popupItems) {
 return PopupMenuButton<String>(
  onSelected: (value) {
    popupItems.firstWhere((e) => e.title == value).onTap();
  },
  itemBuilder: (context) => popupItems
      .map((item) => PopupMenuItem<String>(
            value: item.title,
            child: Text(
              item.title,
            ),
          ))
      .toList(),
    );
   }
 }
感情废物 2025-01-15 23:50:15

你可以尝试这样做:

https://dartpad.dev/?id=a3f9002a37cbacc2cfae46174cbd2eba

您可以添加任何状态管理来替换 FutureBuilder,但是这是合乎逻辑的方法。

我希望它有帮助。

you can try to do something like this:

https://dartpad.dev/?id=a3f9002a37cbacc2cfae46174cbd2eba

you can add any state management to replace the FutureBuilder but this is the logical approach.

I hope it is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文