buildEditWidget method

Widget? buildEditWidget(
  1. DataGridRow dataGridRow,
  2. RowColumnIndex rowColumnIndex,
  3. GridColumn column,
  4. CellSubmit submitCell,
)

Called to obtain the widget when a cell is moved into edit mode.

The following example shows how to override this method and return the widget for specific column.


TextEditingController editingController = TextEditingController();

dynamic newCellValue;

@override
Widget? buildEditWidget(DataGridRow dataGridRow,
    RowColumnIndex rowColumnIndex, GridColumn column,
    CellSubmit submitCell) {
  // To set the value for TextField when cell is moved into edit mode.
  final String displayText = dataGridRow
      .getCells()
      .firstWhere((DataGridCell dataGridCell) =>
  dataGridCell.columnName == column.columnName)
      .value
      ?.toString() ??
      '';

  /// Returning the TextField with the numeric keyboard configuration.
  if (column.columnName == 'id') {
    return Container(
        padding: const EdgeInsets.all(8.0),
        alignment: Alignment.centerRight,
        child: TextField(
          autofocus: true,
          controller: editingController..text = displayText,
          textAlign: TextAlign.right,
          decoration: const InputDecoration(
              contentPadding: EdgeInsets.all(0),
              border: InputBorder.none,
              isDense: true),
          inputFormatters: [
            FilteringTextInputFormatter.allow(RegExp('[0-9]'))
          ],
          keyboardType: TextInputType.number,
          onChanged: (String value) {
            if (value.isNotEmpty) {
              newCellValue = int.parse(value);
            } else {
              newCellValue = null;
            }
          },
          onSubmitted: (String value) {
            /// Call [CellSubmit] callback to fire the canSubmitCell and
            /// onCellSubmit to commit the new value in single place.
            submitCell();
          },
        ));
  }
}

Call the cellSubmit function whenever you are trying to save the cell values. When you call this method, it will call canSubmitCell and onCellSubmit methods. So, your usual cell value updation will be done in single place.

Implementation

Widget? buildEditWidget(DataGridRow dataGridRow,
    RowColumnIndex rowColumnIndex, GridColumn column, CellSubmit submitCell) {
  return null;
}