简体   繁体   中英

How to get selected row index of a dynamic datatable flutter

I have a dynamic dataTable where the is data is dispalyed. Now i need click on the row and pass related data to a new class. This is what i have tried, how to i get the correct index of the clicked row.

    rows: snapshot.data.rowData.map<DataRow>((e) {
     return DataRow(
        cells: e.map<DataCell>((e) => DataCell(Text(e))).toList(),
        onSelectChanged: (bool selected) {
            if (selected) {
        Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => new ImageGridAndListViewScreen(
                        zoomData:snapshot.data.dataList[index].NextLevelData))//here unable to get the selected row index
            );
          }
 
        },

      );
    }).toList(),

You need to write a for loop over snapshot.data.rowData list in order to get index to be used inside onSelectChanged . Something like this.

  List<DataRow> buildListOfDataRows(BuildContext context, WardsData wardsData){

    List<Ward> wards = wardsData.listOfWards;
    List<DataRow> dataRows = [];

    for(int i = 0; i < wards.length; i++) {

      String floorName = wards[i].floorName;
      int ot = wards[i].oxygenBedTotal;
      int oa = wards[i].oxygenBedAvailable;

      DataRow row = DataRow(
        cells: [
          DataCell(Text(floorName)),
          DataCell(Text('$ot')),
          DataCell(Text('$oa')),
        ],
        onSelectChanged: (_){
          showUpdatedByInfoPopUp(context, wards[i]);
        },
      );

      dataRows.add(row);
    }

    return dataRows;
  }

And use this function inside DataTable to build rows.

rows: buildListOfDataRows(context, wardsData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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