Articles in this section
Category / Section

How to enable cell editing with enter in Flutter DataTable (SfDataGrid)?.

4 mins read

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,
      ),
    );
  }

You can download this example on GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied