setColumnWidth method

void setColumnWidth(
  1. String fieldName,
  2. double newWidth
)

Set a column's width by field name. Equivalent to web's rownumColumn.setWidth(estimatedWidth). Works for any column (fixed or data). No-op if field not found or width unchanged. O(N columns) scan — negligible cost, touches zero row data.

Implementation

void setColumnWidth(String fieldName, double newWidth) {
  // Search fixed columns first
  final fixedIndex = _state.fixedColumns.indexWhere((col) => col.field == fieldName);
  if (fixedIndex >= 0) {
    if (_state.fixedColumnWidths[fixedIndex] == newWidth) return; // already correct
    final newFixedWidths = List<double>.from(_state.fixedColumnWidths);
    newFixedWidths[fixedIndex] = newWidth;
    _state = _state.copyWith(
      fixedColumnWidths: newFixedWidths,
      columnWidths: [...newFixedWidths, ..._state.dataColumnWidths],
    );
    notifyListeners();
    return;
  }

  // Search data columns
  final dataIndex = _state.dataColumns.indexWhere((col) => col.field == fieldName);
  if (dataIndex >= 0) {
    if (_state.dataColumnWidths[dataIndex] == newWidth) return; // already correct
    final newDataWidths = List<double>.from(_state.dataColumnWidths);
    newDataWidths[dataIndex] = newWidth;
    _state = _state.copyWith(
      dataColumnWidths: newDataWidths,
      columnWidths: [..._state.fixedColumnWidths, ...newDataWidths],
    );
    notifyListeners();
  }
}