adjustScroll method

void adjustScroll(
  1. int viewportHeight
)

Adjusts the scroll offset to ensure the currently selected item is visible within the viewportHeight.

Implementation

void adjustScroll(int viewportHeight) {
  final listView = widget as ListView;
  final itemCount = listView.children.length;
  if (itemCount <= 0 || viewportHeight <= 0) return;
  selectedIndex = selectedIndex.clamp(0, itemCount - 1);

  if (selectedIndex < scrollOffset) {
    scrollOffset = selectedIndex;
  } else if (selectedIndex >= scrollOffset + viewportHeight) {
    scrollOffset = selectedIndex - viewportHeight + 1;
  }
}