PageView jumpTo 不关闭动画/用户输入

PageView jumpTo without dismissing animation / input of user

我正在尝试更正我的 PageView 的一个页面,而用户正在更改页面。 不幸的是,所有可能的页面更改操作(jumpTo()animateTo()jumpToPage())都会取消当前用户输入(或动画)。看起来像:

有没有办法在用户动画浏览页面的同时更改页面?这样输出可能看起来像:

常规操作关闭所有动画和用户输入。如文档中所述:

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

为了存档您正在尝试执行的操作,您可以使用以下 class 扩展名:

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);
    }
  }
}

现在您可以像 jumpTo() 一样调用 smoothJumpTo(),但不会关闭活动动画,例如用户输入。

final SmoothPageController pageController = SmoothPageController(initialPage: 0);