computeCellWidth method

  1. @protected
double computeCellWidth(
  1. GridColumn column,
  2. DataGridRow row,
  3. Object? cellValue,
  4. TextStyle textStyle,
)

Calculates the width of the cell based on the DataGridCell.value. You can override this method to perform the custom calculation for width.

If you want to calculate the width based on different TextStyle, you can override this method and return the super method with the required TextStyle. Set the custom ColumnSizer to SfDataGrid.columnSizer property.

The following example show how to pass the different TextStyle for width calculation,

class CustomColumnSizer extends ColumnSizer {
@override
 double computeCellWidth(GridColumn column, DataGridRow row, Object cellValue,
     TextStyle textStyle) {
  TextStyle style = textStyle;
  if (column.columnName == 'Name')
   style = TextStyle(fontSize: 20, fontStyle: FontStyle.italic);
 return super.computeCellWidth(column, row, cellValue, style);
}
}

The auto size is calculated based on default TextStyle of the datagrid.

Implementation

@protected
double computeCellWidth(GridColumn column, DataGridRow row, Object? cellValue,
    TextStyle textStyle) {
  final DataGridConfiguration dataGridConfiguration =
      _dataGridStateDetails!();
  final int rowIndex = grid_helper.resolveToRowIndex(dataGridConfiguration,
      effectiveRows(dataGridConfiguration.source).indexOf(row));

  return _calculateTextSize(
          column: column,
          value: cellValue,
          rowIndex: rowIndex,
          textStyle: textStyle,
          width: double.infinity)
      .width
      .ceilToDouble();
}