我想Datatable
通过使用Mysql作为我的数据库来实现快速增长,它可以正常运行,但是它还会循环数据表和项目计数,例如下面的这张图。我一直在尝试使用map,所有数据都存在于数据表中,但是它循环了相同的数据。
图像:
我的代码:
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();
},
),
谢谢,我真的很感激!:)
如果要在同一张卡中显示多行,并且同时希望将该卡作为列表的元素,请查看以下代码:
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();
},);
}
}
这是我的设备上结果的图片
是的,我知道,但是我不希望我的所有数据都乱七八糟,我只想要三个。这就是为什么我放3,即使我放了snapshot.data.length。它复制了我的数据表,我希望它复制数据行。但是无论如何,谢谢您的帮助!!!
你应得的赏金
您能帮我从数据库中获取2个项目吗,因为它可以获取全部
排序请看这里:medium.com/@aervadiyayash510/datatable-in-flutter-916ef8081651
至于只得到两个项目,对不起我没用过mysql,也许有办法