How to Keep Keyboard Open in False CanSubmitCell in Flutter DataTable?
In this article, we’ll demonstrate how to keep the keyboard open when canSubmitCell returns false in a Flutter DataTable.
First, initialize the SfDataGrid widget with the necessary properties. In the TextField’s onSubmitted callback, which is triggered when presses Done or Enter on the keyboard, call your asynchronous canSubmitCell 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.
@override
Widget? buildEditWidget(
DataGridRow dataGridRow,
RowColumnIndex rowColumnIndex,
GridColumn column,
CellSubmit submitCell,
) {
final String displayText =
dataGridRow
.getCells()
.firstWhereOrNull(
(dataCell) => dataCell.columnName == column.columnName,
)
?.value
?.toString() ??
'';
final bool isNumericType =
column.columnName == 'ID' || column.columnName == 'Salary';
newCellValue = null;
editingController.text = displayText;
final focusNode = FocusNode();
return StatefulBuilder(
builder: (context, setState) {
return Container(
padding: const EdgeInsets.all(8.0),
alignment:
isNumericType ? Alignment.centerRight : Alignment.centerLeft,
child: TextField(
autofocus: true,
controller: editingController,
focusNode: focusNode,
textAlign: isNumericType ? TextAlign.right : TextAlign.left,
keyboardType:
isNumericType ? TextInputType.number : TextInputType.text,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(0, 0, 0, 8.0),
),
onChanged: (value) {
if (value.isNotEmpty) {
newCellValue = isNumericType ? int.tryParse(value) : value;
} else {
newCellValue = null;
}
},
onSubmitted: (value) async {
// Ask if cell can be submitted
bool canSubmit = await canSubmitCell(
dataGridRow,
rowColumnIndex,
column,
);
if (canSubmit) {
// Triggers onCellSubmit and allows keyboard to close.
submitCell();
} else {
// Keep focus, prevent keyboard from hiding.
focusNode.requestFocus();
}
},
),
);
},
);
}
@override
Future<bool> canSubmitCell(
DataGridRow dataGridRow,
RowColumnIndex rowColumnIndex,
GridColumn column,
) async {
if (column.columnName == 'ID' && newCellValue == null) {
return false;
} else {
return true;
}
}
You can download this example on GitHub.
Conclusion
I hope you enjoyed learning about How to keep keyboard open when canSubmitCell is false in Flutter DataTable.
You can refer to our Flutter DataTable 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 DataTable 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