scrollTargetToCenter method

void scrollTargetToCenter(
  1. Size tabCenterSize,
  2. int targetIndex,
  3. List<Size>? sizeList,
  4. ScrollController? scrollController, {
  5. Duration? duration,
})

滚动目标索引的项到中间位置

Implementation

void scrollTargetToCenter(Size tabCenterSize, int targetIndex,
    List<Size>? sizeList, ScrollController? scrollController,
    {Duration? duration}) {
  if (targetIndex == lastIndex) return;
  var targetItemScrollOffset =
      getTargetItemScrollEndOffset(sizeList, targetIndex);
  var tabBarSize = getTabBarSize(sizeList);

  double animateToOffset = 0;

  if (direction == Axis.horizontal) {
    animateToOffset = targetItemScrollOffset.dx -
        sizeList![targetIndex].width / 2 -
        tabCenterSize.width;
  } else {
    animateToOffset = targetItemScrollOffset.dy -
        sizeList![targetIndex].height / 2 -
        tabCenterSize.height;
  }

  if (animateToOffset <= 0) {
    animateToOffset = 0;
  } else {
    if (direction == Axis.horizontal) {
      if (animateToOffset + tabCenterSize.width >
          tabBarSize.width - tabCenterSize.width) {
        if (tabBarSize.width > tabCenterSize.width * 2) {
          animateToOffset = tabBarSize.width - tabCenterSize.width * 2;
        } else {
          animateToOffset = 0;
        }
      }
    } else {
      if (animateToOffset + tabCenterSize.height >
          tabBarSize.height - tabCenterSize.height) {
        if (tabBarSize.height > tabCenterSize.height * 2) {
          animateToOffset = tabBarSize.height - tabCenterSize.height * 2;
        } else {
          animateToOffset = 0;
        }
      }
    }
  }

  lastIndex = targetIndex;

  if (duration == null) {
    scrollController?.jumpTo(animateToOffset);
  } else {
    scrollController?.animateTo(animateToOffset,
        duration: duration, curve: Curves.easeIn);
  }
}