shouldRecalculateColumnWidths method

  1. @protected
bool shouldRecalculateColumnWidths()

Called whenever you call notifyListeners or notifyDataSourceListeners in the DataGridSource class. If you want to recalculate all columns width (may be when underlying data gets changed), return true.

Returning true may impact performance as the column widths are recalculated again (whenever the notifyListeners is called).

If you are aware that column widths are going to be same whenever underlying data changes, return 'false' from this method.

Note: Column widths will be recalculated automatically whenever a new instance of DataGridSource is assigned to SfDataGrid.

class EmployeeDataSource extends DataGridSource {
  @override
  List<DataGridRow> get rows => _employees
      .map<DataGridRow>((dataRow) => DataGridRow(cells: [
            DataGridCell<int>(columnName: 'id', value: dataRow.id),
            DataGridCell<String>(columnName: 'name', value: dataRow.name),
            DataGridCell<String>(
                columnName: 'designation', value: dataRow.designation),
            DataGridCell<int>(columnName: 'salary', value: dataRow.salary),
          ]))
      .toList();

  @override
  bool shouldRecalculateColumnWidths() {
    return true;
  }

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataCell) {
          return Text(dataCell.value.toString());
        }).toList());
  }
}

Implementation

@protected
bool shouldRecalculateColumnWidths() {
  return false;
}