flutterflutter-pageview

PageView jumpTo without dismissing animation / input of user


I am trying to correct a page of my PageView, while the user is changing pages. Unfortunately, all possible page change operations (jumpTo(), animateTo(), jumpToPage()) dismiss the current user input (or animation). Which looks like:

Is there any way to change pages, while the user animates the pageview? So that the output might look like:


Solution

  • The usual operations dismiss all animation and user inputs. As also described in the documentation:

    Any active animation is canceled. If the user is currently scrolling, that action is canceled.

    In order to archive what you are trying to do you can use the following class extension:

    class SmoothPageController extends PageController {
      SmoothPageController({
        int initialPage = 0,
        bool keepPage = true,
        double viewportFraction = 1.0,
      }) : super(
              initialPage: initialPage,
              keepPage: keepPage,
              viewportFraction: viewportFraction,
            );
    
      /// Jumps the scroll position from its current value to the given value,
      /// without animation, and without checking if the new value is in range.
      /// Any active animation is being kept. If the user is currently scrolling,
      /// that action is kept and will proceed as expected.
      /// Copied from ScrollController.
      void smoothJumpTo(double value) {
        assert(positions.isNotEmpty,
            'ScrollController not attached to any scroll views.');
        for (final ScrollPosition position in List<ScrollPosition>.of(positions)) {
          position.correctPixels(value);
        }
      }
    }
    
    

    Now you can call smoothJumpTo() just like jumpTo() but without dismissing active animations, such as the user input.

    final SmoothPageController pageController = SmoothPageController(initialPage: 0);