简体   繁体   English

如何在 Flutter 的 PaginatedDataTable 中使用 DataRows 列表?

[英]How to use a list of DataRows in a PaginatedDataTable in Flutter?

I have a DataTable where the user can dynamically add and remove DataRows.我有一个DataTable ,用户可以在其中动态添加和删除 DataRows。 Because of that the DataTable can get really big.因此,DataTable 会变得非常大。 To improve performance I want do use PaginatedDataTable.为了提高性能,我想使用 PaginatedDataTable。 But this Widget needs an extra class for the datasource.但是这个 Widget 需要一个额外的数据源类。 The basic code for that looks like that:其基本代码如下所示:

class DataSource extends DataTableSource {

  int _selectedCount = 0;

  @override
  int get rowCount => list.length;

  @override
  bool get isRowCountApproximate => false;

  @override
  int get selectedRowCount => _selectedCount;

  @override
  DataRow getRow(int index) {

    return DataRow.byIndex(
        index: index,
        cells: <DataCell>[
          DataCell(Text('1')),
          DataCell(Text('2')),
          DataCell(Text('3')),
          DataCell(Text('4')),
        ]);
  }
}

In my old Code I used a DataTable, where I had all the DataRows in a list and it worked fine.在我的旧代码中,我使用了一个 DataTable,其中我将所有 DataRows 放在一个列表中并且运行良好。 Here is a snippet from the Code with the list:这是代码中包含列表的片段:

class ExerciseTable extends StatefulWidget {

  @override
  _ExerciseTableState createState() => _ExerciseTableState();
}

class _ExerciseTableState extends State<ExerciseTable> {
  ExerciseDataSource _rowsDataSource;

  List<DataRow> _rowList = [
    DataRow(cells: <DataCell>[
          DataCell(Text('1')),
          DataCell(Text('2')),
          DataCell(Text('3')),
          DataCell(Text('4')),
    ]),
  ];

  void _addRow() {
    _rowList.insert(0,
       DataRow(cells: <DataCell>[
          DataCell(Text('1')),
          DataCell(Text('2')),
          DataCell(Text('3')),
          DataCell(Text('4')),
        ])
    );
  }

  void _removeRow() {
    setState(() {
      _rowList.removeAt(0);
    });
  }

Now I want to use the same list with the DataRows for the PaginatedDataTable but how can I integrate the list in the 'DataSource' Class?现在我想对 PaginatedDataTable 的 DataRows 使用相同的列表,但是如何将列表集成到“DataSource”类中? I appreciate every answer, would be great if someone knows how to do that :)我很感激每一个答案,如果有人知道怎么做那就太好了:)

Integrating PaginatedDataTable on customs needs might be a litte confusing, but you can pass your data source list to the DataSource class (through the constructor), and on the getRow method, using the index you can iterate through your data.在海关需求上集成 PaginatedDataTable 可能有点令人困惑,但您可以将数据源列表传递给 DataSource 类(通过构造函数),并且在 getRow 方法上,使用可以迭代数据的索引。

An example might be:一个例子可能是:

@Override
DataRow getRow(int index) {
 final currentData = yourDataList[index]
 return DataRow.byIndex(
  index: index,
  cells: <DataCell>[
   DataCell(Text('${currentData.name}'},
  ],
 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM