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 width =
      this.width ?? controller.getTabBarSize(sizeList).width - right - left;

  double targetLeft =
      controller.getTargetItemScrollStartOffset(sizeList, index).dx;

  if (this.width != null) {
    targetLeft = targetLeft +
        (sizeList?[index].width ?? 0 - this.width!) / 2 -
        this.width! / 2;
  }
  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);
    } else {
      rate = (_animation.value - left) / (targetLeft - left);
    }

    if (this.width == null) {
      targetRight = controller.getTabBarSize(sizeList).width -
          _animation.value -
          width -
          (sizeList![index].width - width) * rate;
    } else {
      targetRight =
          controller.getTabBarSize(sizeList).width - _animation.value - width;
    }

    notifier.value = IndicatorPosition(_animation.value, targetRight, 0, 0);
    controller.forEachListenerCallback();
  });
  _animationController!.forward();
}