如何将数据从 extends DataTableSource 类传递到 StateFull 类

问题描述

我有 PaginatedDataTable 并且我将源的 PaginatedDataTable 的属性放到另一个类中..这是代码

class MainPage extends StatefulWidget {

  MainPage({Key key}) : super(key: key);

  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
   return Scaffold(
body: PaginatedDataTable(
        header: Center(
                            child: Text(
                          'List Data'
                        )),onRowsPerPageChanged:...
                        columns: <DataColumn>[
                          DataColumn(label: Text('No.')),DataColumn(label: Text('Data 1')),DataColumn(label: Text('Data 2'))
                        ],source: mysource,rowsPerPage:...
                      )

   )
  }
}

对于我的来源:

class MySource extends DataTableSource {
...
}

但让我感到困惑的是...在我处理 class MySource extends DataTableSource 中的数据后,我不知道如何从那里传递数据以在 StateFulWidget 的 MainPage 中调用它......所以有没有从 class MySource extends DataTableSource

调用 class MainPage extends StatefulWidget 内部数据的方法

解决方法

为您的问题编写代码,希望对您有所帮助。

class _MainPageState extends State<MainPage> {
      @override
      Widget build(BuildContext context) {
        MySource mySource = new MySource(["ab","bc","de","ef"]);
        return Scaffold(
          body: Column(
          children: [
            //Widget form
            PaginatedDataTable(
              header: Center(child: Text('List Data')),columns: <DataColumn>[
                DataColumn(label: Text('No.')),DataColumn(label: Text('Data 1')),DataColumn(label: Text('Action')),],source: mySource,),));
      }
    }
    
    class MySource extends DataTableSource {
      List<String> value;
      MySource(this.value) {
        print(value);
      }
    
      @override
      DataRow getRow(int index) {
        // TODO: implement getRow
        return DataRow.byIndex(
              index: index,cells: [
                DataCell(Text('$index')),DataCell(Text(value[index])),DataCell(InkWell(
          onTap:(){
            //fill the form above the table and after user fill it,the data inside the table will be refreshed
          },child: Text("Click"),);
      }
    
      @override
      // TODO: implement isRowCountApproximate
      bool get isRowCountApproximate => false;
    
      @override
      // TODO: implement rowCount
      int get rowCount => value.length;
    
      @override
      // TODO: implement selectedRowCount
      int get selectedRowCount =>0;
    }

enter image description here