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

问题描述 投票:0回答:2

我有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

flutter state flutter-web stateful paginateddatatable
2个回答
1
投票

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

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


0
投票

Can you take user's input instead of a pre-defined list of strings

© www.soinside.com 2019 - 2024. All rights reserved.