animateToPage method

dynamic animateToPage(
  1. int page,
  2. int duration
)

Animating page to the mentioned page

Known Issue : First we have to jump to the previous screen.

Current Behaviour : Lets say there are 3 pages, current page is Red and next is Blue and and last is Green. if page in this method came to ne 2 i.e., we need to animate directly to Green Page but currently I have to jump to page 1 i.e., Blue and than perform Animation using updateSlide

Required Behaviour : I Don't want Blue to be there in between the transition of Red and Green i.e., If we are animating activePageIndex should be 0 and nextPageIndex should be 2, which is not possible through current implementation.

If you encounter this and have suggestions don't forget to raise an Issue.

Not making it for Public usage for now due to the mentioned Issue _animateDirectlyToPage(int page, int duration) { if (isInProgress || activePageIndex == page) return; isInProgress = true; activePageIndex = page - 1; nextPageIndex = page; if (activePageIndex < 0) { activePageIndex = 0; jumpToPage(page); return; } _timer = Timer.periodic(const Duration(milliseconds: 1), (t) { if (t.tick < duration / 2) { updateSlide(SlideUpdate(SlideDirection.rightToLeft, t.tick / duration, 1, UpdateType.dragging)); } else if (t.tick < duration) { updateSlide(SlideUpdate(SlideDirection.rightToLeft, t.tick / duration, 1, UpdateType.animating)); } else { updateSlide(SlideUpdate( SlideDirection.rightToLeft, 1, 1, UpdateType.doneAnimating)); t.cancel(); isInProgress = false; } }); }

Animating to the Page in One-by-One manner Required parameters :

  • page, the page index you want to animate to.
  • duration, of Duration type, for complete animation

Implementation

animateToPage(int page, int duration) {
  if (isInProgress || activePageIndex == page) return;
  isInProgress = true;
  int diff = 0;
  _timer?.cancel();
  _timerInner?.cancel();
  if (activePageIndex < page) {
    diff = page - activePageIndex;
    int newDuration = duration ~/ diff;
    _timer = Timer.periodic(Duration(milliseconds: newDuration), (callback) {
      _timerInner = Timer.periodic(const Duration(milliseconds: 1), (t) {
        if (t.tick < newDuration / 2) {
          updateSlide(SlideUpdate(SlideDirection.rightToLeft,
              t.tick / newDuration, positionSlideIcon, UpdateType.dragging));
        } else if (t.tick < newDuration) {
          updateSlide(SlideUpdate(SlideDirection.rightToLeft,
              t.tick / newDuration, positionSlideIcon, UpdateType.animating));
        } else {
          updateSlide(SlideUpdate(SlideDirection.rightToLeft, 1,
              positionSlideIcon, UpdateType.doneAnimating));
          t.cancel();
        }
      });
      if (callback.tick >= diff) {
        callback.cancel();
        isInProgress = false;
      }
    });
  } else {
    diff = activePageIndex - page;
    int newDuration = duration ~/ diff;
    _timer = Timer.periodic(Duration(milliseconds: newDuration), (callback) {
      _timerInner = Timer.periodic(const Duration(milliseconds: 1), (t) {
        if (t.tick < newDuration / 2) {
          updateSlide(SlideUpdate(SlideDirection.leftToRight,
              t.tick / newDuration, positionSlideIcon, UpdateType.dragging));
        } else if (t.tick < newDuration) {
          updateSlide(SlideUpdate(SlideDirection.leftToRight,
              t.tick / newDuration, positionSlideIcon, UpdateType.animating));
        } else {
          updateSlide(SlideUpdate(SlideDirection.leftToRight, 1,
              positionSlideIcon, UpdateType.doneAnimating));
          t.cancel();
        }
      });
      if (callback.tick >= diff) {
        callback.cancel();
        isInProgress = false;
      }
    });
  }
}