scrollToTab method

void scrollToTab(
  1. dynamic selectedValue
)

Scrolls to the tab with the given value.

Implementation

void scrollToTab(dynamic selectedValue) {
  if (!scrollController.hasClients) return;

  final key = tabKeys[selectedValue];
  if (key?.currentContext == null) return;

  final RenderBox? renderBox = key!.currentContext!.findRenderObject() as RenderBox?;
  if (renderBox == null) return;

  final position = renderBox.localToGlobal(Offset.zero);

  final scrollOffset = axis == Axis.horizontal
      ? position.dx - 50 // Add some padding
      : position.dy - 50;

  final targetOffset = scrollController.offset + scrollOffset;

  scrollController.animateTo(
    targetOffset.clamp(0.0, scrollController.position.maxScrollExtent),
    duration: const Duration(milliseconds: 300),
    curve: Curves.easeInOut,
  );
}