scrollToCell method

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 getRowIndex = grid_helper.resolveToRowIndex(
        dataGridConfiguration, rowIndex.toInt());
    final int getColumnIndex = grid_helper.resolveToGridVisibleColumnIndex(
        dataGridConfiguration, columnIndex.toInt());
    double verticalOffset =
        grid_helper.getVerticalOffset(dataGridConfiguration, getRowIndex);
    double horizontalOffset = grid_helper.getHorizontalOffset(
        dataGridConfiguration, getColumnIndex);

    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,
        getRowIndex);

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

    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);
  }
}