flutter ScrollPhysics applyPhysicsToUserOffset 的理解

37 阅读1分钟

默认实现

image.png

参数 第二个参数是用户手指滑动的距离。

这个方法主要用来决定如何处理用户的手指滑动距离。

以BouncingScrollPhysics的实现为例:

image.png

    if (!position.outOfRange) {
      return offset;
    }

未超出边界全量处理手指移动距离。

    final double overscrollPastStart = math.max(position.minScrollExtent - position.pixels, 0.0);
    final double overscrollPastEnd = math.max(position.pixels - position.maxScrollExtent, 0.0);

overscrollPastStart 判断是否上越界

overscrollPastEnd 判断是否下越界

后面的代码主要是根据越界情况,通过计算决定返回的Offset。从而实现阻尼效果。