animateToPage method

Future<void> animateToPage(
  1. int page, {
  2. Duration? duration = const Duration(milliseconds: 300),
  3. Curve? curve = Curves.linear,
})
override

Animates the controlled CarouselSlider from the current page to the given page.

The animation lasts for the given duration and follows the given curve. The returned Future resolves when the animation completes.

Implementation

Future<void> animateToPage(int page,
    {Duration? duration = const Duration(milliseconds: 300),
    Curve? curve = Curves.linear}) async {
  final bool isNeedResetTimer = _state!.options.pauseAutoPlayOnManualNavigate;
  if (isNeedResetTimer) {
    _state!.onResetTimer();
  }
  final index = getRealIndex(_state!.pageController!.page!.toInt(),
      _state!.realPage - _state!.initialPage, _state!.itemCount);
  int smallestMovement = page - index;
  if (_state!.options.enableInfiniteScroll &&
      _state!.itemCount != null &&
      _state!.options.animateToClosest) {
    if ((page - index).abs() > (page + _state!.itemCount! - index).abs()) {
      smallestMovement = page + _state!.itemCount! - index;
    } else if ((page - index).abs() >
        (page - _state!.itemCount! - index).abs()) {
      smallestMovement = page - _state!.itemCount! - index;
    }
  }
  _setModeController();
  await _state!.pageController!.animateToPage(
      _state!.pageController!.page!.toInt() + smallestMovement,
      duration: duration!,
      curve: curve!);
  if (isNeedResetTimer) {
    _state!.onResumeTimer();
  }
}