indicatorScrollToIndex method

  1. @override
void indicatorScrollToIndex(
  1. int index,
  2. List<Size>? sizeList,
  3. Duration duration,
  4. TickerProvider vsync,
  5. ValueNotifier<IndicatorPosition> notifier,
)
override

Implementation

@override
void indicatorScrollToIndex(
    int index,
    List<Size>? sizeList,
    Duration duration,
    TickerProvider vsync,
    ValueNotifier<IndicatorPosition> notifier) {
  double left = notifier.value.left;
  double right = notifier.value.right;
  double top = notifier.value.top;
  double bottom = notifier.value.bottom;

  if (controller.direction == Axis.horizontal) {
    double width = controller.getTabBarSize(sizeList).width - right - left;
    double targetLeft =
        controller.getTargetItemScrollStartOffset(sizeList, index).dx;
    if (targetLeft == left) return;

    _animationController =
        AnimationController(duration: duration, vsync: vsync);

    _animation =
        Tween(begin: left, end: targetLeft).animate(_animationController!);

    _animation.addListener(() {
      double? rate = 0;
      double targetRight = 0;
      if (left > targetLeft) {
        rate = 1 - (targetLeft - _animation.value) / (targetLeft - left);
        targetRight = controller.getTabBarSize(sizeList).width -
            _animation.value -
            width -
            (sizeList![index].width - width) * rate;
      } else {
        rate = (_animation.value - left) / (targetLeft - left);
        targetRight = controller.getTabBarSize(sizeList).width -
            _animation.value -
            width -
            (sizeList![index].width - width) * rate!;
      }

      notifier.value = IndicatorPosition(_animation.value, targetRight, 0, 0);
      controller.forEachListenerCallback();
    });
  } else {
    double height = controller.getTabBarSize(sizeList).height - bottom - top;
    double targetTop =
        controller.getTargetItemScrollStartOffset(sizeList, index).dy;
    if (targetTop == top) return;

    _animationController =
        AnimationController(duration: duration, vsync: vsync);

    _animation =
        Tween(begin: top, end: targetTop).animate(_animationController!);

    _animation.addListener(() {
      double? rate = 0;
      double targetBottom = 0;
      if (top > targetTop) {
        rate = 1 - (targetTop - _animation.value) / (targetTop - top);
        targetBottom = controller.getTabBarSize(sizeList).height -
            _animation.value -
            height -
            (sizeList![index].height - height) * rate;
      } else {
        rate = (_animation.value - top) / (targetTop - top);
        targetBottom = controller.getTabBarSize(sizeList).height -
            _animation.value -
            height -
            (sizeList![index].height - height) * rate!;
      }

      notifier.value =
          IndicatorPosition(0, 0, _animation.value, targetBottom);
      controller.forEachListenerCallback();
    });
  }

  _animationController!.forward();
}