setScrollTabItemToCenter method

void setScrollTabItemToCenter(
  1. int index,
  2. List<Size>? sizeList,
  3. Size tabBarViewCenterSize,
  4. ScrollController? scrollController,
  5. Duration? duration,
)

Implementation

void setScrollTabItemToCenter(
  int index,
  List<Size>? sizeList,
  Size tabBarViewCenterSize,
  ScrollController? scrollController,
  Duration? duration,
) {
  if (index == _lastIndex) {
    return;
  }

  _targetIndex = index;

  final totalTabBarSize = getTotalTabBarSize(sizeList);
  final targetEndOffset = getTargetEndOffset(sizeList, index);

  final size = sizeList![index];
  final sizeWidth = size.width;
  final sizeHeight = size.height;
  final totalWidth = totalTabBarSize.width;
  final totalHeight = totalTabBarSize.height;
  final tabBarViewCenterWidth = tabBarViewCenterSize.width;
  final tabBarViewCenterHeight = tabBarViewCenterSize.height;

  double animateToOffset = 0;

  if (direction == Axis.horizontal) {
    animateToOffset =
        (targetEndOffset.dx - sizeWidth / 2) - tabBarViewCenterWidth;
  }

  if (direction == Axis.vertical) {
    animateToOffset =
        (targetEndOffset.dy - sizeHeight / 2) - tabBarViewCenterHeight;
  }

  // 处理 start 边界
  if (animateToOffset <= 0) {
    animateToOffset = 0;
  }

  // 处理 end 边界
  if (animateToOffset > 0 && direction == Axis.horizontal) {
    if (totalWidth - tabBarViewCenterWidth * 2 < animateToOffset) {
      totalWidth > tabBarViewCenterWidth * 2
          ? animateToOffset = totalWidth - tabBarViewCenterWidth * 2
          : animateToOffset = 0;
    }
  }

  // 处理 end 边界
  if (animateToOffset > 0 && direction == Axis.vertical) {
    if (totalHeight - tabBarViewCenterHeight * 2 < animateToOffset) {
      totalHeight > tabBarViewCenterHeight * 2
          ? animateToOffset = totalHeight - tabBarViewCenterHeight * 2
          : animateToOffset = 0;
    }
  }

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

  _lastIndex = index;
  _targetIndex = index;
  _currentIndex = index;
}