scrollOffsetToReveal static method

double? scrollOffsetToReveal({
  1. required int selectedIndex,
  2. required double itemHeight,
  3. required double viewportHeight,
  4. required double currentOffset,
})

Compute the scroll offset needed to reveal the selected item in a fixed-height list.

Returns null if the item is already visible. The caller should call scrollController.jumpTo(offset) with the returned value.

This is a pure calculation with no Flutter dependency.

Implementation

static double? scrollOffsetToReveal({
  required int selectedIndex,
  required double itemHeight,
  required double viewportHeight,
  required double currentOffset,
}) {
  final target = selectedIndex * itemHeight;
  if (target < currentOffset) {
    return target;
  }
  if (target + itemHeight > currentOffset + viewportHeight) {
    return target + itemHeight - viewportHeight;
  }
  return null;
}