2

I have PaginatedDataTable and I put property of source's PaginatedDataTable to another class.. here is the code

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:...
                      )

   )
  }
}

and for the mysource:

class MySource extends DataTableSource {
...
}

but the thing that makes me confused is... after I process my data inside class MySource extends DataTableSource I don't know how to pass the data from there to call it inside MainPage of StateFulWidget.. so is there a way to call data inside class MySource extends DataTableSource from class MainPage extends StatefulWidget

wahyu
  • 1,679
  • 5
  • 35
  • 73
  • Please clear your question. – Nandan Wewhare Dec 20 '20 at 09:25
  • Sorry for creating the misunderstanding, actually I need to call something like state inside DataTableSource and that state will be I used to fill the form above the table (like saving new data and editing the old data and then refresh the row of the table with updated data).. as we know that PaginatedDataTable is inside of StatefulWidget and the source of PaginatedDataTable is outside StatefulWidget, for more detail I have added the comment from @MevlütGür's answer – wahyu Dec 20 '20 at 10:57

1 Answers1

1

Write code for your question ,I hope that will helpful.

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

wahyu
  • 1,679
  • 5
  • 35
  • 73
Mevlüt Gür
  • 155
  • 1
  • 10
  • Hi, thank you @MevlütGür, actually I have button inside DataCell and I have a form above the table, so when user click the button I need to process a form above the table – wahyu Dec 20 '20 at 10:38
  • I have edited your answer and the thing that makes me confused is how to call something like state inside ```InkWell``` and I will use that state to save or edit the form from data row of table – wahyu Dec 20 '20 at 10:47
  • 1
    As I understand it this is wrong way for state concept.You can apply [this video](https://www.youtube.com/watch?v=n2Dav9ONJsY&ab_channel=MTECHVIRAL) or tis [web page](https://androidmonks.com/datatable-flutter/). – Mevlüt Gür Dec 20 '20 at 11:50
  • thank you very much @MevlütGür I will try to take a look to your video and article link – wahyu Dec 20 '20 at 12:26
  • @uyhaW, did you find any solution? – Hiren Mar 20 '21 at 06:06
  • @Hiren, I use a package: ```data_tables``` it helps me also to paginate datatable, tell me if you need the sample code that I used – wahyu Mar 22 '21 at 01:20
  • @uyhaW, thanks for quick reply. I will try with data_tables. If it is easy and simple then please provide the sample code that you have used. Thanks. – Hiren Mar 24 '21 at 08:27
  • @Hiren, here is how I used it https://gist.github.com/coba-cobaoke/dc75a99e217a311ac198221c4537a297 – wahyu Mar 25 '21 at 01:18