indicatorScrollToIndex method

  1. @override
void indicatorScrollToIndex(
  1. int index,
  2. List<Size>? sizeList,
  3. ValueNotifier<ScrollProgress> scroller,
  4. ValueNotifier<IndicatorPosition> notifier,
)
override

Implementation

@override
void indicatorScrollToIndex(
  int index,
  List<Size>? sizeList,
  ValueNotifier<ScrollProgress> scroller,
  ValueNotifier<IndicatorPosition> notifier,
) {
  final itemSize = sizeList![index];
  final totalTabBarSize = controller.getTotalTabBarSize(sizeList);
  final targetEndOffset = controller.getTargetEndOffset(sizeList, index);
  double totalTabBarHeight = totalTabBarSize.height;
  double totalTabBarWidth = totalTabBarSize.width;
  double sizeHeight = itemSize.height;
  double sizeWidth = itemSize.width;

  double indicatorWidth = sizeWidth;
  double indicatorHeight = sizeHeight;

  if (width != null) {
    indicatorWidth = min(
      min(
        sizeWidth + width!,
        sizeWidth,
      ),
      width! <= 0 ? double.infinity : width!,
    );
  }

  if (height != null) {
    indicatorHeight = min(
      min(
        sizeHeight + height!,
        sizeHeight,
      ),
      height! <= 0 ? double.infinity : height!,
    );
  }

  if (indicatorWidth < 0) {
    indicatorWidth = 0;
  }

  if (indicatorHeight < 0) {
    indicatorHeight = 0;
  }

  if (controller.direction == Axis.horizontal) {
    final dir = scroller.value.dir;
    final progress = scroller.value.progress;
    final targetIndex = scroller.value.targetIndex;
    final currentIndex = scroller.value.currentIndex;

    double old = notifier.value.left ?? 0;
    double dist = targetEndOffset.dx - (sizeWidth + indicatorWidth) / 2;
    double left = old + (dist - old) * (dir == 1 ? progress : 1 - progress);
    double right = totalTabBarWidth - left - indicatorWidth;

    notifier.value = IndicatorPosition(
      top,
      left,
      right,
      bottom,
      indicatorWidth,
      indicatorHeight,
    );

    controller.callProgressListener(
      ScrollProgress(
        currentIndex: currentIndex,
        targetIndex: targetIndex,
        progress: progress,
        dir: dir,
      ),
    );
  }

  if (controller.direction == Axis.vertical) {
    final dir = scroller.value.dir;
    final progress = scroller.value.progress;
    final targetIndex = scroller.value.targetIndex;
    final currentIndex = scroller.value.currentIndex;

    double old = notifier.value.top ?? 0;
    double dist = targetEndOffset.dy - (sizeHeight + indicatorHeight) / 2;
    double top = old + (dist - old) * (dir == 1 ? progress : 1 - progress);
    double bottom = totalTabBarHeight - top - indicatorHeight;

    notifier.value = IndicatorPosition(
      top,
      left,
      right,
      bottom,
      indicatorWidth,
      indicatorHeight,
    );

    controller.callProgressListener(
      ScrollProgress(
        currentIndex: currentIndex,
        targetIndex: targetIndex,
        progress: progress,
        dir: dir,
      ),
    );
  }
}