微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将数据从 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。