animateToPage method
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 pageController = _state!.pageController;
if (pageController == null || !pageController.hasClients) {
return;
}
final bool isNeedResetTimer = _state!.options.pauseAutoPlayOnManualNavigate;
if (isNeedResetTimer) {
_state!.onResetTimer();
}
final currentPage = pageController.page?.toInt() ?? _state!.realPage;
final index = getRealIndex(currentPage,
_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 pageController.animateToPage(
currentPage + smallestMovement,
duration: duration!,
curve: curve!);
if (isNeedResetTimer) {
_state!.onResumeTimer();
}
}