Category / Section
How to restrict swiping in a particular direction in Flutter DataGrid (SfDataGrid)?
2 mins read
In this article, you can learn about how to restrict swiping in a particular direction in Flutter DataGrid.
Initialize the SfDataGrid widget with all the necessary properties. Achieve this by utilizing the SfDataGrid.onSwipeStart callback event. Inside the onSwipeStart callback, return false when the swipeDirection is DataGridRowSwipeDirection.startToEnd to prevent left-to-right swiping, and return false when the swipeDirection is DataGridRowSwipeDirection.endToStart to prevent right-to-left swiping.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Syncfusion DataGrid Demo')),
body: SfDataGrid(
source: _employeeDataSource,
columns: getColumns,
columnWidthMode: ColumnWidthMode.fill,
allowSwiping: true,
onSwipeStart: (swipeStartDetails) {
if (swipeStartDetails.swipeDirection ==
DataGridRowSwipeDirection.endToStart) {
return false;
} else {
return true;
}
},
endSwipeActionsBuilder:
(BuildContext context, DataGridRow row, int rowIndex) {
return GestureDetector(
onTap: () {
_employeeDataSource.dataGridRows.removeAt(rowIndex);
_employeeDataSource.updateDataGridSource();
},
child: Container(
color: Colors.redAccent,
child: const Center(
child: Icon(Icons.delete),
)));
},
),
);
}