I am relatively new tro flutter. I am trying to create a DataTable that the user could Tap on and take some action based on which row was clicked. However, when the user taps on any cell, it picks up the last value (bottom left of the table) irrespective of which cell or row is Tap'ed. Also tried onSelectChanged
List<DataRow> dataRows = [];
String cellValue = '';
final formatter = NumberFormat('#,##0;(#,##0)');
for (int z = 0; z < widget.data.length; z++) {
dataRows.add(DataRow(
cells: [],
onSelectChanged: (selected) {
if (selected!) {
debugPrint('row-selected: $cellValue');
}
}));
int ii = 0;
for (int i = 0; i < noOfColumns; i++) {
ii = i + 1;
if (columnTypes[i] == 'N') {
cellValue = formatter.format(double.parse(widget.data[z]['col$ii']));
} else {
cellValue = widget.data[z]['col$ii'].toString();
}
dataRows[z].cells.add(
DataCell(
Text('--$cellValue'),
onTap: () => {
debugPrint(
"onTap Pressed ====>$cellValue-------${TimeOfDay.now()}")
},
),
);
}
}
The way you coded it makes it take the
cellValue
that it was last because it was declared in the outer scope. Try introducing a local variable in the local scope instead by replacingwith