Articles in this section
Category / Section

How to Set Keyboard Type For a Column in Flutter DataTable?

3 mins read

In this article, we will show how to set keyboard type for a column in Flutter DataTable.

Initialize the SfDataGrid widget with the necessary properties. By default, SfDataGrid does not load any widget when a cell enters edit mode. To enable editing, you must return the appropriate widget using the buildEditWidget method in the DataGridSource class.

In this case, we use a TextField widget for editing. Based on the column name, we can determine whether a numeric or alphabetic keyboard should appear for value entry. The keyboardType property of the TextField specifies the type of keyboard to display (e.g., TextInputType.number for numeric input).

@override
Widget? buildEditWidget(DataGridRow dataGridRow,
    RowColumnIndex rowColumnIndex, GridColumn column, CellSubmit submitCell) {
  final String displayText = dataGridRow
          .getCells()
          .firstWhereOrNull((DataGridCell dataGridCell) =>
              dataGridCell.columnName == column.columnName)
          ?.value
          ?.toString() ??
      '';
  newCellValue = null;
  
  final bool isNumericType =
      column.columnName == 'ID' || column.columnName == 'Salary';
      
  return Container(
    padding: const EdgeInsets.all(8.0),
    alignment: isNumericType ? Alignment.centerRight : Alignment.centerLeft,
    child: TextField(
      autofocus: true,
      controller: editingController..text = displayText,
      textAlign: isNumericType ? TextAlign.right : TextAlign.left,
      decoration: const InputDecoration(
        contentPadding: EdgeInsets.fromLTRB(0, 0, 0, 8.0),
      ),
      keyboardType:
          isNumericType ? TextInputType.number : TextInputType.text,
      onChanged: (String value) {
        if (value.isNotEmpty) {
          if (isNumericType) {
            newCellValue = int.parse(value);
          } else {
            newCellValue = value;
          }
        } else {
          newCellValue = null;
        }
      },
      onSubmitted: (String value) {
        submitCell();
      },
    ),
  );
}

View the GitHub sample here.

Conclusion
I hope you enjoyed learning about how to implement unique field support in Syncfusion® 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 always happy to assist you!

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