|
1 | | -# How-to-keep-keyboard-open-when-canSubmitCell-is-false-in-Flutter-DataTable |
2 | | -This demo shows How to keep keyboard open when canSubmitCell is false in Flutter DataTable |
| 1 | +# How to keep keyboard open when canSubmitCell is false in Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we’ll demonstrate how to keep the keyboard open when canSubmitCell returns false in a [Flutter DataTable](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 4 | + |
| 5 | +First, initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with the necessary properties. In the TextField’s onSubmitted callback, which is triggered when the user presses Done or Enter on the keyboard, call your asynchronous [canSubmitCell](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/canSubmitCell.html) method to determine whether editing should end. If it returns false, request focus back to the TextField. This ensures that the keyboard remains visible when editing is not allowed to conclude. |
| 6 | + |
| 7 | +```dart |
| 8 | + @override |
| 9 | + Widget? buildEditWidget( |
| 10 | + DataGridRow dataGridRow, |
| 11 | + RowColumnIndex rowColumnIndex, |
| 12 | + GridColumn column, |
| 13 | + CellSubmit submitCell, |
| 14 | + ) { |
| 15 | + final String displayText = |
| 16 | + dataGridRow |
| 17 | + .getCells() |
| 18 | + .firstWhereOrNull( |
| 19 | + (dataCell) => dataCell.columnName == column.columnName, |
| 20 | + ) |
| 21 | + ?.value |
| 22 | + ?.toString() ?? |
| 23 | + ''; |
| 24 | +
|
| 25 | + final bool isNumericType = |
| 26 | + column.columnName == 'ID' || column.columnName == 'Salary'; |
| 27 | +
|
| 28 | + newCellValue = null; |
| 29 | + editingController.text = displayText; |
| 30 | + final focusNode = FocusNode(); |
| 31 | +
|
| 32 | + return StatefulBuilder( |
| 33 | + builder: (context, setState) { |
| 34 | + return Container( |
| 35 | + padding: const EdgeInsets.all(8.0), |
| 36 | + alignment: |
| 37 | + isNumericType ? Alignment.centerRight : Alignment.centerLeft, |
| 38 | + child: TextField( |
| 39 | + autofocus: true, |
| 40 | + controller: editingController, |
| 41 | + focusNode: focusNode, |
| 42 | + textAlign: isNumericType ? TextAlign.right : TextAlign.left, |
| 43 | + keyboardType: |
| 44 | + isNumericType ? TextInputType.number : TextInputType.text, |
| 45 | + decoration: const InputDecoration( |
| 46 | + contentPadding: EdgeInsets.fromLTRB(0, 0, 0, 8.0), |
| 47 | + ), |
| 48 | + onChanged: (value) { |
| 49 | + if (value.isNotEmpty) { |
| 50 | + newCellValue = isNumericType ? int.tryParse(value) : value; |
| 51 | + } else { |
| 52 | + newCellValue = null; |
| 53 | + } |
| 54 | + }, |
| 55 | + onSubmitted: (value) async { |
| 56 | + // Ask if cell can be submitted |
| 57 | + bool canSubmit = await canSubmitCell( |
| 58 | + dataGridRow, |
| 59 | + rowColumnIndex, |
| 60 | + column, |
| 61 | + ); |
| 62 | + if (canSubmit) { |
| 63 | + // Triggers onCellSubmit and allows keyboard to close. |
| 64 | + submitCell(); |
| 65 | + } else { |
| 66 | + // Keep focus, prevent keyboard from hiding. |
| 67 | + focusNode.requestFocus(); |
| 68 | + } |
| 69 | + }, |
| 70 | + ), |
| 71 | + ); |
| 72 | + }, |
| 73 | + ); |
| 74 | + } |
| 75 | +
|
| 76 | + @override |
| 77 | + Future<bool> canSubmitCell( |
| 78 | + DataGridRow dataGridRow, |
| 79 | + RowColumnIndex rowColumnIndex, |
| 80 | + GridColumn column, |
| 81 | + ) async { |
| 82 | + if (column.columnName == 'ID' && newCellValue == null) { |
| 83 | + return false; |
| 84 | + } else { |
| 85 | + return true; |
| 86 | + } |
| 87 | + } |
| 88 | +``` |
| 89 | + |
| 90 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-keep-keyboard-open-when-canSubmitCell-is-false-in-Flutter-DataTable). |
0 commit comments