温馨提示:本文翻译自stackoverflow.com,查看原文请点击:laravel - Flutter List View Builder Item Count Datatable with MySql Database
api datatable flutter laravel mysql

laravel - Flutter List View Builder项目计数数据表与MySql数据库

发布于 2021-04-11 07:33:54

我想Datatable通过使用Mysql作为我的数据库来实现快速增长,它可以正常运行,但是它还会循环数据表和项目计数,例如下面的这张图。我一直在尝试使用map,所有数据都存在于数据表中,但是它循环了相同的数据。

图像:

img

我的代码:

 FutureBuilder(
                    future: fetchClients(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return Container(
                          child: ListView.builder(
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: 3,
                            shrinkWrap: true,
                            itemBuilder: (BuildContext context, index) {
                              Contacts contacts = snapshot.data[index];
                              return Card(
                                child: Column(
                                  children: [
                                    Container(
                                      decoration: BoxDecoration(
                                          borderRadius: BorderRadius.only(
                                            topLeft: Radius.circular(5.0),
                                            topRight: Radius.circular(5.0),
                                          ),
                                          color: Color(0xfff6f8fa),
                                          border: Border.all(
                                            color: Color(0xffd5d8dc),
                                            width: 1,
                                          )),
                                      padding: EdgeInsets.only(
                                          top: 13.0, left: 13.0, bottom: 13.0),
                                      child: Row(
                                        children: [
                                          Icon(
                                            FontAwesomeIcons.addressBook,
                                            color: backgroundColor,
                                            size: 15,
                                          ),
                                          Text(
                                            '   Clients Overviews',
                                            style: TextStyle(
                                                fontWeight: FontWeight.w700),
                                          )
                                        ],
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          DataTable(
                                            sortColumnIndex: 1,
                                            sortAscending: true,
                                            columns: [
                                              DataColumn(
                                                label: Text('Name'),
                                                numeric: false,
                                                tooltip: 'Name',
                                              ),
                                              DataColumn(
                                                label: Text('Address'),
                                                numeric: false,
                                                tooltip: 'Address',
                                              ),
                                              DataColumn(
                                                label: Text('Number'),
                                                numeric: false,
                                                tooltip: 'Number',
                                              ),
                                            ],
                                            rows: [
                                              DataRow(
                                                cells: [
                                                  DataCell(
                                                    Text('${contacts.name}'),
                                                  ),
                                                  DataCell(
                                                    Text('${contacts.address}'),
                                                  ),
                                                  DataCell(
                                                    Text('${contacts.mobile}'),
                                                  ),
                                                ].toList(),
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            },
                          ),
                        );
                      }
                      return CircularProgressIndicator();
                    },
                  ),

谢谢,我真的很感激!:)

查看更多

提问者
xAtifx
被浏览
0
farouk osama 2020-08-12 14:11

如果要在同一张卡中显示多行,并且同时希望将该卡作为列表的元素,请查看以下代码:

ListView(
          physics: NeverScrollableScrollPhysics(),
          children: <Widget>[
            MyCustomCard(),
            //add same card or another cards
          ],
        ),

你可以创建自己的窗口小部件以简化工作,在我们的示例中,它是MyCustomCard例如

class MyCustomCard extends StatefulWidget {
  @override
  _MyCustomCardState createState() => _MyCustomCardState();
}

class _MyCustomCardState extends State<MyCustomCard> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: fetchClients(),
        builder: (BuildContext context,  snapshot) {
      if (snapshot.hasData) {
        List<Contacts> contacts = snapshot.data;
       return Card(
          child: Column(
            children: [
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(5.0),
                      topRight: Radius.circular(5.0),
                    ),
                    color: Color(0xfff6f8fa),
                    border: Border.all(
                      color: Color(0xffd5d8dc),
                      width: 1,
                    )),
                padding: EdgeInsets.only(
                    top: 13.0, left: 13.0, bottom: 13.0),
                child: Row(
                  children: [
                    Icon(
                      FontAwesomeIcons.addressBook,
                      color: backgroundColor,
                      size: 15,
                    ),
                    Text(
                      '   Clients Overviews',
                      style: TextStyle(fontWeight: FontWeight.w700),
                    )
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DataTable(
                      sortColumnIndex: 1,
                      sortAscending: true,
                      columns: [
                        DataColumn(
                          label: Text('Name'),
                          numeric: false,
                          tooltip: 'Name',
                        ),
                        DataColumn(
                          label: Text('Address'),
                          numeric: false,
                          tooltip: 'Address',
                        ),
                        DataColumn(
                          label: Text('Number'),
                          numeric: false,
                          tooltip: 'Number',
                        ),
                      ],
                      rows: [
                        for (var item in contacts)
                          DataRow(
                            cells: [
                              DataCell(
                                Text('${item.name}'),
                              ),
                              DataCell(
                                Text('${item.address}'),
                              ),
                              DataCell(
                                Text('${item.number}'),
                              ),
                            ].toList(),
                          ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      }
      else return CircularProgressIndicator();
    },);

  }
}

这是我的设备上结果的图片

在此处输入图片说明