scrollToCell method Null safety

Future<void> scrollToCell(
  1. double rowIndex,
  2. double columnIndex,
  3. {bool canAnimate = false,
  4. DataGridScrollPosition rowPosition = DataGridScrollPosition.start,
  5. DataGridScrollPosition columnPosition = DataGridScrollPosition.start}
)

Scrolls the SfDataGrid to the given row and column index.

If you want animation on scrolling, you can pass true as canAnimate argument.

Also, you can control the position of a row when it comes to view by passing the DataGridScrollPosition as an argument for rowPosition where as you can pass DataGridScrollPosition as an argument for columnPosition to control the position of a column.

Implementation

Future<void> scrollToCell(double rowIndex, double columnIndex,
    {bool canAnimate = false,
    DataGridScrollPosition rowPosition = DataGridScrollPosition.start,
    DataGridScrollPosition columnPosition =
        DataGridScrollPosition.start}) async {
  if (_dataGridStateDetails != null) {
    final DataGridConfiguration dataGridConfiguration =
        _dataGridStateDetails!();
    final ScrollAxisBase scrollRows =
        dataGridConfiguration.container.scrollRows;
    final ScrollAxisBase scrollColumns =
        dataGridConfiguration.container.scrollColumns;

    if (rowIndex > dataGridConfiguration.container.rowCount ||
        columnIndex > scrollColumns.lineCount ||
        (rowIndex.isNegative && columnIndex.isNegative)) {
      return;
    }

    final int _rowIndex = grid_helper.resolveToRowIndex(
        dataGridConfiguration, rowIndex.toInt());
    final int _columnIndex = grid_helper.resolveToGridVisibleColumnIndex(
        dataGridConfiguration, columnIndex.toInt());
    double verticalOffset =
        grid_helper.getVerticalOffset(dataGridConfiguration, _rowIndex);
    double horizontalOffset =
        grid_helper.getHorizontalOffset(dataGridConfiguration, _columnIndex);

    if (dataGridConfiguration.textDirection == TextDirection.rtl &&
        columnIndex == -1) {
      horizontalOffset = dataGridConfiguration.container.extentWidth -
                  dataGridConfiguration.viewWidth -
                  horizontalOffset >
              0
          ? dataGridConfiguration.container.extentWidth -
              dataGridConfiguration.viewWidth -
              horizontalOffset
          : 0;
    }

    verticalOffset = grid_helper.resolveScrollOffsetToPosition(
        rowPosition,
        scrollRows,
        verticalOffset,
        dataGridConfiguration.viewHeight,
        scrollRows.headerExtent,
        scrollRows.footerExtent,
        dataGridConfiguration.rowHeight,
        dataGridConfiguration.container.verticalOffset,
        _rowIndex);

    horizontalOffset = grid_helper.resolveScrollOffsetToPosition(
        columnPosition,
        scrollColumns,
        horizontalOffset,
        dataGridConfiguration.viewWidth,
        scrollColumns.headerExtent,
        scrollColumns.footerExtent,
        dataGridConfiguration.defaultColumnWidth,
        dataGridConfiguration.container.horizontalOffset,
        _columnIndex);

    grid_helper.scrollVertical(
        dataGridConfiguration, verticalOffset, canAnimate);
    // Need to add await for the horizontal scrolling alone, to avoid the delay time between vertical and horizontal scrolling.
    await grid_helper.scrollHorizontal(
        dataGridConfiguration, horizontalOffset, canAnimate);
  }
}