颤振:如何在 Listview.builder 中使用 DataTable

时间:2021-05-19 09:50:28

标签: flutter material-ui flutter-layout

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

1 个答案:

答案 0 :(得分:0)

      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;
    }