Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:如何在pageview中禁用向左滚动?_Flutter_Flutter Layout_Flutter Pageview - Fatal编程技术网

Flutter 颤振:如何在pageview中禁用向左滚动?

Flutter 颤振:如何在pageview中禁用向左滚动?,flutter,flutter-layout,flutter-pageview,Flutter,Flutter Layout,Flutter Pageview,今天,当我开发我的应用程序时,我发现我需要一个功能,它只允许用户滚动到右边的页面,而不允许用户滚动到左边的页面,PageController不提供任何允许我实现的功能,所以这就是我来这里的原因 我也访问了这个链接: ,但它没有包含任何解释,您知道在不知道代码在做什么的情况下使用其他人的代码是痛苦的,对吗?所以请帮助我^_^ 您可以创建自己的ScrollPhysics以将滚动锁定到某个方向,也可以使用lib来帮助您 下面是一个CustomScrollPhysic的示例,它只支持右滚动 class

今天,当我开发我的应用程序时,我发现我需要一个功能,它只允许用户滚动到右边的页面,而不允许用户滚动到左边的页面,PageController不提供任何允许我实现的功能,所以这就是我来这里的原因

我也访问了这个链接:

,但它没有包含任何解释,您知道在不知道代码在做什么的情况下使用其他人的代码是痛苦的,对吗?所以请帮助我^_^

您可以创建自己的ScrollPhysics以将滚动锁定到某个方向,也可以使用lib来帮助您

下面是一个CustomScrollPhysic的示例,它只支持右滚动

class CustomScrollPhysics extends ScrollPhysics {
  CustomScrollPhysics({ScrollPhysics parent}) : super(parent: parent);

  bool isGoingLeft = false;

  @override
  CustomScrollPhysics applyTo(ScrollPhysics ancestor) {
    return CustomScrollPhysics(parent: buildParent(ancestor));
  }

  @override
  double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
    isGoingLeft = offset.sign < 0;
    return offset;
  }

  @override
  double applyBoundaryConditions(ScrollMetrics position, double value) {
    assert(() {
      if (value == position.pixels) {
        throw FlutterError(
          '$runtimeType.applyBoundaryConditions() was called redundantly.\n'
          'The proposed new position, $value, is exactly equal to the current position of the '
          'given ${position.runtimeType}, ${position.pixels}.\n'
          'The applyBoundaryConditions method should only be called when the value is '
          'going to actually change the pixels, otherwise it is redundant.\n'
          'The physics object in question was:\n'
          '  $this\n'
          'The position object in question was:\n'
          '  $position\n');
      }
      return true;
    }());
    if (value < position.pixels && position.pixels <= position.minScrollExtent)
      return value - position.pixels;
    if (position.maxScrollExtent <= position.pixels && position.pixels < value)
      return value - position.pixels;
    if (value < position.minScrollExtent &&
        position.minScrollExtent < position.pixels)

      return value - position.minScrollExtent;

    if (position.pixels < position.maxScrollExtent &&
        position.maxScrollExtent < value)
      return value - position.maxScrollExtent;

    if (!isGoingLeft) {
      return value - position.pixels;
    }
    return 0.0;
  }
}
一些可以更好地解释物理原理的参考文献


非常感谢您的回复,但您是否有办法在不添加新软件包的情况下解决我的问题,因为我的应用程序已经有200 mb大了,并且害怕添加新软件包@_@我添加了一个新的代码示例!我认为它可以帮助你是的,谢谢你的病人,但是解释一下会很好,或者你知道关于这方面的任何教程,这样我就可以深入了解它的工作原理了吗不客气!我添加了一些参考资料,可以更好地解释物理现象。此代码的主要功能是applyBoundaryConditions,它使用位置和滚动值作为参数来评估用户是否可以滚动。
PageView.builder(
  physics: CustomScrollPhysics(),
);