onSelectedRowChanged method Null safety

  1. @override
void onSelectedRowChanged()
override

Called when the selectedRow property in SfDataGrid.controller is changed.

Implementation

@override
void onSelectedRowChanged() {
  final DataGridConfiguration dataGridConfiguration =
      _dataGridStateDetails!();

  if (dataGridConfiguration.selectionMode == SelectionMode.none) {
    return;
  }

  final DataGridRow? newValue = dataGridConfiguration.controller.selectedRow;

  bool canClearSelections() =>
      _selectedRows.isNotEmpty &&
      dataGridConfiguration.selectionMode != SelectionMode.multiple;

  //If newValue is negative we have clear the whole selection data.
  //In multiple case we shouldn't to clear the collection as well
  // source properties.
  if (newValue == null && canClearSelections()) {
    _clearSelectedRow(dataGridConfiguration);
    notifyListeners();
    return;
  }

  final int recordIndex =
      effectiveRows(dataGridConfiguration.source).indexOf(newValue!);
  final int rowIndex =
      grid_helper.resolveToRowIndex(dataGridConfiguration, recordIndex);

  if (rowIndex < grid_helper.getHeaderIndex(dataGridConfiguration)) {
    return;
  }

  if (!_selectedRows.contains(newValue)) {
    //In multiple case we shouldn't to clear the collection as
    // well source properties.
    if (canClearSelections()) {
      _clearSelectedRow(dataGridConfiguration);
    }

    if (dataGridConfiguration.navigationMode == GridNavigationMode.cell) {
      _addCurrentCell(newValue, dataGridConfiguration,
          isSelectionChanging: true);
    } else {
      final int columnIndex =
          grid_helper.resolveToStartColumnIndex(dataGridConfiguration);
      final int rowIndex =
          selection_helper.resolveToRowIndex(dataGridConfiguration, newValue);
      dataGridConfiguration.currentCell
          ._updateCurrentRowColumnIndex(rowIndex, columnIndex);
    }

    _addSelection(newValue, dataGridConfiguration);
    notifyListeners();
  }
}