How to get cell detail when right-clicking a cell in Flutter DataGrid?
In this article, we will show you how to get cell details when right-clicking a cell in Flutter DataGrid.
Initialize the SfDataGrid widget with all required properties. Details about the right-clicked cell, such as rowColumnIndex, column, localPosition, and globalPosition, can be accessed through DataGridCellTapDetails, which is passed as a parameter in the onCellSecondaryTap callback. Furthermore, you can retrieve the value of the tapped cell from the effectiveRows collection using the RowColumnIndex.rowIndex property.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: SfDataGrid(
source: employeeDataSource,
columnWidthMode: ColumnWidthMode.fill,
onCellSecondaryTap: (details) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Tapped Cell Information'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(
'Column Index: ${details.rowColumnIndex.columnIndex}\n'
'Row Index: ${details.rowColumnIndex.rowIndex}\n'
'Column Name: ${details.column.columnName}',
),
const SizedBox(height: 8),
// Fetch details of the tapped cell value, excluding header cells.
if (details.rowColumnIndex.rowIndex != 0)
Text(
'Cell Value: ${employeeDataSource.effectiveRows[details.rowColumnIndex.rowIndex - 1].getCells()[details.rowColumnIndex.columnIndex].value}',
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
columns: <GridColumn>[
GridColumn(
columnName: 'id',
label: Container(
padding: const EdgeInsets.all(16.0),
alignment: Alignment.center,
child: const Text(
'ID',
))),
GridColumn(
columnName: 'name',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Name'))),
GridColumn(
columnName: 'designation',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text(
'Designation',
overflow: TextOverflow.ellipsis,
))),
GridColumn(
columnName: 'salary',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Salary'))),
],
),
);
}
Conclusion
I hope you enjoyed learning about how to get cell detail when right-clicking a cell in Flutter DataGrid.
You can refer to our Flutter DataTable feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter DataTable example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!