How to Enable Cell Editing with Enter in Flutter DataGrid?
In this article, we will show you how to enable cell editing with enter in Flutter DataTable.
STEP 1: Create a custom selection manager class by extending RowSelectionManager. In the handleKeyEvent method, check if the Enter key is pressed using keyEvent. Then, call DataGridController.beginEdit inside WidgetsBinding.instance.addPostFrameCallback, using DataGridController.currentCell.
class CustomSelectionManager extends RowSelectionManager {
CustomSelectionManager(this.dataGridController);
DataGridController dataGridController;
@override
Future<void> handleKeyEvent(KeyEvent keyEvent) async {
if (keyEvent.logicalKey == LogicalKeyboardKey.enter) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
dataGridController.beginEdit(dataGridController.currentCell);
});
}
super.handleKeyEvent(keyEvent);
}
}
STEP 2: Initialize the SfDataGrid widget with all the required properties. Create an instance of the CustomSelectionManager class and assign it to the SfDataGrid.selectionManager property. Create an instance of the DataGridController and assign it to the SfDataGrid.controller property.
List<Employee> _employees = <Employee>[];
late EmployeeDataSource _employeeDataSource;
DataGridController dataGridController = DataGridController();
late CustomSelectionManager customSelectionManager;
@override
void initState() {
super.initState();
_employees = getEmployeeData();
_employeeDataSource = EmployeeDataSource(_employees, context);
customSelectionManager = CustomSelectionManager(dataGridController);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
body: SfDataGrid(
source: _employeeDataSource,
controller: dataGridController,
allowEditing: true,
selectionManager: customSelectionManager,
navigationMode: GridNavigationMode.cell,
selectionMode: SelectionMode.single,
columns: <GridColumn>[
GridColumn(
columnName: 'id',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerRight,
child: const Text('ID'),
),
),
GridColumn(
columnName: 'name',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: const Text('Name'),
),
),
GridColumn(
allowEditing: false,
columnName: 'designation',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: const Text('Designation', overflow: TextOverflow.ellipsis),
),
),
GridColumn(
columnName: 'salary',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerRight,
child: const Text('Salary'),
),
),
],
columnWidthMode: ColumnWidthMode.fill,
),
);
}
Conclusion
I hope you enjoyed learning how to enable cell editing with enter in Flutter DataGrid.
You can refer to our Flutter DataGrid 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 DataGrid 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!