【问题标题】:Pull on refresh without pulling animation in ListView在 ListView 中拉动刷新而不拉动动画
【发布时间】:2020-02-09 15:51:49
【问题描述】:

我在 RefreshIndicator 和 SmartRefresher 中使用了 ListView,但问题是当我拉下它时,它会为拉动设置动画。我不想显示拉动动画。我曾尝试使用 clampscrollphysics 禁用拉动动画,但如果我使用它,刷新将不起作用。所以请让我知道如何进行拉动刷新,它会显示刷新指示器,也会刷新但不会显示带有列表视图的下拉动画

【问题讨论】:

    标签: flutter


    【解决方案1】:

    将自定义物理类应用到您的列表视图中。

      ListView.builder(
              physics: Custom2ScrollPhysics()
      )
    

    Custom2ScrollPhysics 类

        class Custom2ScrollPhysics extends ScrollPhysics {
          /// Creates scroll physics that prevent the scroll offset from exceeding the
          /// bounds of the content..
          const Custom2ScrollPhysics({ScrollPhysics parent}) : super(parent: parent);
    
          @override
          Custom2ScrollPhysics applyTo(ScrollPhysics ancestor) {
            return Custom2ScrollPhysics(parent: buildParent(ancestor));
          }
    
          @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) // underscroll
              return value - position.pixels;
            if (position.maxScrollExtent <= position.pixels && position.pixels < value) // overscroll
              return value - position.pixels;
            if (value < position.minScrollExtent && position.minScrollExtent < position.pixels) // hit top edge
              return value - position.minScrollExtent;
            if (position.pixels < position.maxScrollExtent && position.maxScrollExtent < value) // hit bottom edge
              return value - position.maxScrollExtent;
            return 0.0;
          }
    
          @override
          Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
            final Tolerance tolerance = this.tolerance;
            if (position.outOfRange) {
              double end;
              if (position.pixels > position.maxScrollExtent) end = position.maxScrollExtent;
              if (position.pixels < position.minScrollExtent) end = position.minScrollExtent;
              assert(end != null);
              return ScrollSpringSimulation(spring, position.pixels, end, min(0.0, velocity), tolerance: tolerance);
            }
            if (velocity.abs() < tolerance.velocity) return null;
            if (velocity > 0.0 && position.pixels >= position.maxScrollExtent) return null;
            if (velocity < 0.0 && position.pixels <= position.minScrollExtent) return null;
            return ClampingScrollSimulation(
              position: position.pixels,
              velocity: velocity,
              tolerance: tolerance,
            );
          }
        }
    

    【讨论】:

    • 不工作。也许我做错了!你能给我上面例子gif的源代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多