Flutter DataTable - Tap on row
Solution:
you can use onSelectChanged property from DataRow.
rows: items
.map(
(itemRow) => DataRow(
onSelectChanged: (bool selected) {
if (selected) {
log.add('row-selected: ${itemRow.index}');
}
},
cells: [
// ..
],
),
Try this :
DataTable(
showCheckboxColumn: false, // <-- this is important
columns: [
DataColumn(label: Text('FirstName')),
DataColumn(label: Text('LastName')),
],
rows:[
DataRow(
cells: [
DataCell(Text(obj['user1'])),
DataCell(Text(obj['name-a'])),
],
onSelectChanged: (newValue) {
print('row 1 pressed');
},
),
DataRow(
cells: [
DataCell(Text(obj['user2'])),
DataCell(Text(obj['name-b'])),
],
onSelectChanged: (newValue) {
print('row 2 pressed');
},
),
]
),
Hope this helps. Thanks
Each DataCell
has an onTap
callback. You could use this without the unhideable checkbox appearing on your table rows.
For example
DataCell(Text(itemrow.itemname),
onTap: () {
// Your code here
})
This works for me. If you want the onTap
to work for the entire DataRow
instead of only a DataCell
, you could just add the logic to the onTap
of each DataCell
and get the desired result.