Flutter 如何遍历地图列表并在FlatterListView中显示文本小部件?

Flutter 如何遍历地图列表并在FlatterListView中显示文本小部件?,flutter,listview,dart,widget,Flutter,Listview,Dart,Widget,我对flatter非常陌生,我试图通过内容列表中的for循环显示listview,但我能够打印一个列表行,这是一次迭代,因为返回只触发一次。如何遍历完整列表并显示所有listview元素? 以下是地图内容列表 List<Map<String, String>> users = [ {"name": "Staff 1", "status": "Online"}, {"

我对flatter非常陌生,我试图通过内容列表中的for循环显示listview,但我能够打印一个列表行,这是一次迭代,因为返回只触发一次。如何遍历完整列表并显示所有listview元素? 以下是地图内容列表

 List<Map<String, String>> users = [
    {"name": "Staff 1", "status": "Online"},
    {"name": "Staff 2", "status": "Online"},
    {"name": "Staff 3", "status": "Offline"},
    {"name": "Vehicle 1", "status": "Available"},
    {"name": "Vehicle 2", "status": "Unavailable"},
    {"name": "Team 1", "status": "Active"},
    {"name": "Team 2", "status": "Not Active"},
  ];
列出用户=[
{“姓名”:“员工1”,“状态”:“在线”},
{“姓名”:“员工2”,“状态”:“在线”},
{“姓名”:“职员3”,“状态”:“脱机”},
{“名称”:“车辆1”,“状态”:“可用”},
{“名称”:“车辆2”,“状态”:“不可用”},
{“名称”:“团队1”,“状态”:“活动”},
{“名称”:“团队2”,“状态”:“未激活”},
];
下面是我试图显示的listview小部件的代码

  Widget ListViewBuilder() {
        for (var i = 0; i < users.length; i++) {
          print(i);
          return Expanded(
            child: ListView(
              children: <Widget>[
                GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(
                      context,
                      '/chatscreen',
                    );
                  },
                  child: ListTile(
                      leading: CircleAvatar(
                          radius: 20,
                          backgroundImage: AssetImage("assets/profileavatar.png")),
                      title: Text(users[i]['name']),
                      subtitle: Text("User -ID"),
                      trailing: Row(
                          mainAxisAlignment:
                              MainAxisAlignment.spaceBetween, // added line
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            Text(
                              users[i]['status'],
                              style: TextStyle(
                                color: Colors.green,
                                fontSize: 20,
                          ),
                 ),                          
              ],
            ),
          );
        }
      }
Widget ListViewBuilder(){
对于(var i=0;i

这将只显示列表中的第一个元素,如何实现显示列表用户中的所有listview元素?任何建议都会很有帮助

这里的问题是-您将在第一个循环中返回结果。这就是为什么您只获得列表中的第一个元素

您需要在ListView小部件中遍历地图

List<Map<String, String>> users = [
    {"name": "Staff 1", "status": "Online"},
    {"name": "Staff 2", "status": "Online"},
    {"name": "Staff 3", "status": "Offline"},
    {"name": "Vehicle 1", "status": "Available"},
    {"name": "Vehicle 2", "status": "Unavailable"},
    {"name": "Team 1", "status": "Active"},
    {"name": "Team 2", "status": "Not Active"},
  ];

Widget ListViewBuilder(BuildContext context) {
      return Expanded(
        child: ListView(
          children: <Widget>[
            ...users.map( (u) => 
            <Widget> [GestureDetector(
              onTap: () {
                Navigator.pushNamed(
                  context,
                  '/chatscreen',
                );
              },
              child: ListTile(
                  leading: CircleAvatar(
                      radius: 20,
                      backgroundImage: AssetImage("assets/profileavatar.png")),
                  title: Text(u['name']),
                  subtitle: Text("User -ID"),
                  trailing: Row(
                      mainAxisAlignment:
                          MainAxisAlignment.spaceBetween, // added line
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Text(
                          u['status'],
                          style: TextStyle(
                            color: Colors.green,
                            fontSize: 20,
                          ),
                        ),
                        SizedBox(
                          width: 20,
                        ),
                        InkWell(
                          onTap: () {
                            Navigator.pushNamed(
                              context,
                              '/viewlocation',
                            );
                          },
                          child: Icon(
                            Icons.add_location,
                            color: Colors.green,
                            size: 40,
                          ),
                        ),
                        SizedBox(
                          width: 20,
                        ),
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.green,
                              border: Border.all(
                                color: Colors.green,
                              ),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(20))),
                          child: InkWell(
                            onTap: () async {
                              const number =
                                  '08592119XXXX'; //set the number here
                              bool res =
                                  await FlutterPhoneDirectCaller.callNumber(
                                      number);
                            },
                            child: Icon(
                              Icons.call,
                              color: Colors.white,
                              size: 40,
                            ),
                          ),
                        )
                      ])),
            ),
            Divider(
              color: Colors.black,
              endIndent: 10,
              indent: 10,
            )
            ]).expand((element) => element).toList()
            ,
          ],
        ),
      );
 
  }

列出用户=[
{“姓名”:“员工1”,“状态”:“在线”},
{“姓名”:“员工2”,“状态”:“在线”},
{“姓名”:“职员3”,“状态”:“脱机”},
{“名称”:“车辆1”,“状态”:“可用”},
{“名称”:“车辆2”,“状态”:“不可用”},
{“名称”:“团队1”,“状态”:“活动”},
{“名称”:“团队2”,“状态”:“未激活”},
];
小部件ListViewBuilder(构建上下文){
扩大回报(
子:ListView(
儿童:[
…用户.map((u)=>
[手势检测器(
onTap:(){
Navigator.pushNamed(
上下文
“/chatscreen”,
);
},
孩子:ListTile(
领先:CircleAvatar(
半径:20,
背景图片:AssetImage(“assets/profileavatar.png”),
标题:文本(u['name']),
字幕:文本(“用户ID”),
尾随:行(
主轴对准:
MainAxisAlignment.spaceBetween,//添加了行
mainAxisSize:mainAxisSize.min,
儿童:[
正文(
u[“状态”],
样式:TextStyle(
颜色:颜色。绿色,
尺寸:20,
),
),
大小盒子(
宽度:20,
),
墨水池(
onTap:(){
Navigator.pushNamed(
上下文
“/viewlocation”,
);
},
子:图标(
Icons.add_位置,
颜色:颜色。绿色,
尺码:40,
),
),
大小盒子(
宽度:20,
),
容器(
装饰:盒子装饰(
颜色:颜色。绿色,
边界:边界(
颜色:颜色。绿色,
),
边界半径:
边界半径所有(半径圆形(20)),
孩子:InkWell(
onTap:()异步{
常数=
“08592119XXXX”;//在此处设置数字
布尔雷斯酒店=
等待PhoneDirectCaller.callNumber(
数量);
},
子:图标(
图标。呼叫,
颜色:颜色,白色,
尺码:40,
),
),
)
])),
),
分隔器(
颜色:颜色,黑色,
结束缩进:10,
缩进:10,
)
]).expand((元素)=>元素).toList()
,
],
),
);
child: ListView(
          children: <Widget>[
      [GestureDetector, Divider, GestureDetector, Divider]]
child: ListView(
          children: <Widget>[
      GestureDetector, Divider, GestureDetector, Divider]
  Widget listViewBuilder() {
    return Expanded(
        child: ListView.separated(
            itemCount: users.length,
            itemBuilder: (context, i) {
              return GestureDetector(
                onTap: () {
                  Navigator.pushNamed(
                    context,
                    '/chatscreen',
                  );
                },
                child: ListTile(
                    leading: CircleAvatar(
                        radius: 20,
                        backgroundImage:
                            AssetImage("assets/profileavatar.png")),
                    title: Text(users[i]['name']),
                    subtitle: Text("User -ID"),
                    trailing: Row(
                        mainAxisAlignment:
                            MainAxisAlignment.spaceBetween, // added line
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Text(
                            users[i]['status'],
                            style: TextStyle(
                              color: Colors.green,
                              fontSize: 20,
                            ),
                          ),
                          SizedBox(
                            width: 20,
                          ),
                          InkWell(
                            onTap: () {
                              Navigator.pushNamed(
                                context,
                                '/viewlocation',
                              );
                            },
                            child: Icon(
                              Icons.add_location,
                              color: Colors.green,
                              size: 40,
                            ),
                          ),
                          SizedBox(
                            width: 20,
                          ),
                          Container(
                            decoration: BoxDecoration(
                                color: Colors.green,
                                border: Border.all(
                                  color: Colors.green,
                                ),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20))),
                            child: InkWell(
                              onTap: () async {
                                const number =
                                    '08592119XXXX'; //set the number here
                                bool res =
                                    await FlutterPhoneDirectCaller.callNumber(
                                        number);
                              },
                              child: Icon(
                                Icons.call,
                                color: Colors.white,
                                size: 40,
                              ),
                            ),
                          )
                        ])),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider(
                color: Colors.black,
                endIndent: 10,
                indent: 10,
              );
            }));
  }
class CustomListTile extends StatelessWidget {
  final String name, status;

  const CustomListTile({Key key, this.name, this.status}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(
          context,
          '/chatscreen',
        );
      },
      child: ListTile(
        leading: CircleAvatar(
            radius: 20,
            backgroundImage: AssetImage("assets/profileavatar.png")),
        title: Text(name),
        subtitle: Text("User -ID"),
        trailing: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween, // added line
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              status,
              style: TextStyle(
                color: Colors.green,
                fontSize: 20,
              ),
            ),
            SizedBox(
              width: 20,
            ),
            InkWell(
              onTap: () {
                Navigator.pushNamed(
                  context,
                  '/viewlocation',
                );
              },
              child: Icon(
                Icons.add_location,
                color: Colors.green,
                size: 40,
              ),
            ),
            SizedBox(
              width: 20,
            ),
            Container(
              decoration: BoxDecoration(
                  color: Colors.green,
                  border: Border.all(
                    color: Colors.green,
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(20))),
              child: InkWell(
                onTap: () async {
                  const number = '08592119XXXX'; //set the number here
                  // bool res = await FlutterPhoneDirectCaller.callNumber(number);
                },
                child: Icon(
                  Icons.call,
                  color: Colors.white,
                  size: 40,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
class DemoPage extends StatelessWidget {
  DemoPage({
    Key key,
  }) : super(key: key);

  final List<Map<String, String>> users = [
    {"name": "Staff 1", "status": "Online"},
    {"name": "Staff 2", "status": "Online"},
    {"name": "Staff 3", "status": "Offline"},
    {"name": "Vehicle 1", "status": "Available"},
    {"name": "Vehicle 2", "status": "Unavailable"},
    {"name": "Team 1", "status": "Active"},
    {"name": "Team 2", "status": "Not Active"},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [   
          Expanded(
            child: ListView.separated(
              separatorBuilder: (BuildContext context, int index) => Divider(
                color: Colors.black,
                endIndent: 10,
                indent: 10,
              ),
              itemCount: users.length,
              itemBuilder: (context, index) {
                // parsing the maps in the list before passing them into the extracted widget
                // the itemBuilder iterates through the list
                final name = users[index]['name'];
                final status = users[index]['status'];
                return CustomListTile(name: name, status: status);
              },
            ),
          ),
        ],
      ),
    );
  }
}