flutter:如何将DataTable与Listview.builder一起使用



最近我使用flutter和sqflite开发了一个应用程序。用于将数据显示为我在DataTable小部件中使用的表格形式,但当我将DataTable与listView.builder一起使用时,我遇到了一个问题,但它没有按照我想要的方式显示数据。我的意思是,对于我从listview.builder使用的数据库中的红色数据,每次从数据库中获取记录时,它都会重复行和列。但我希望列显示一次,行重复数据库中存在的每个记录。我试图用listview.builder只包装行,但用DataTable是不可能的。有人能指导我如何解决这个问题吗。而且DataTable是否分页

List<Cells> cellsList =[];
List<DataCell> list =[];

Widget dataTable(list) {
for (int i = 0; i < list.length; i++) {
var split = list[i]["created_at"].toString().split("T");
cellsList.add(Cells([
DataCell(dataCells(context, (i + 1).toString())),
DataCell(dataCells(context, list[i]["ticket_no"].toString())),
DataCell(dataCells(context, split[0])),
]));
}

return Padding(
padding: const EdgeInsets.symmetric( vertical: 20),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"My Active Tickets",
style: boldText(context),
),
SizedBox(
height: 10,
),
Flexible(
child: Card(

color: Theme.of(context).primaryColor,elevation: 0,shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),

side: BorderSide(
color: Theme.of(context).primaryTextTheme.bodyText1.color)),
child: CardlessHeadlessPaginatedDataTable(
footer: list.length >=5? true:false,
rowsPerPage:list.length ==0?1:list.length,
columns: <DataColumn>[
DataColumn(label: currencyTabs("Serial No.")),
DataColumn(label: currencyTabs("Ticket No.")),
DataColumn(label: currencyTabs("Buy Time")),
],
source: DataSource(context,cellsList),
),
),
),
],
),
);
}

class Cells{
Cells(this.cells);
List<DataCell> cells = [];
}

Widget currencyTabs(,title) {
return Text(
title,textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15,
),
);
}

import 'package:flutter/material.dart';
import 'package:rio/Model/dataTableModel.dart';
import 'package:rio/widgets/widgets.dart';
class DataSource extends DataTableSource {
final list,cellsList;
DataSource(this.context,this.cellsList) {

// _rows = list;
}

final BuildContext context;


int _selectedCount = 0;

@override
DataRow getRow(int index) {
assert(index >= 0);


if(cellsList.length != index)
{
if(index%2 ==0) {
return DataRow(

color: MaterialStateProperty.all(Theme
.of(context)
.primaryColor),
cells: [
for(int j = 0; j < cellsList[index].cells.length; j++)
cellsList[index].cells[j],

],
);
}else{
return DataRow(
color: MaterialStateProperty.all(Theme
.of(context)
.bottomAppBarColor),
cells: [
for(int j = 0; j < cellsList[index].cells.length; j++)
cellsList[index].cells[j],

],
);
}
}
}

@override
int get rowCount => cellsList.length;

@override
bool get isRowCountApproximate => false;

@override
int get selectedRowCount => _selectedCount;
}

最新更新