onSelectedRowChanged method

  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.
  if (newValue == null && _selectedRows.isNotEmpty) {
    // If selection mode is multiple we need to clear all the selected rows.
    if (dataGridConfiguration.selectionMode == SelectionMode.multiple) {
      _clearSelectedRows(dataGridConfiguration);
    } else {
      _clearSelectedRow(dataGridConfiguration);
    }
    if (dataGridConfiguration.navigationMode == GridNavigationMode.cell) {
      _clearCurrentCell(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();
  }
}