createBallisticSimulation method
创建弹道滚动模拟
该方法用于创建滚动结束后的惯性滚动模拟,控制滚动的减速和回弹效果
参数:
position:当前滚动位置velocity:滚动速度- 返回值:弹道滚动模拟对象,如果不需要模拟则返回null
Implementation
@override
/// 创建弹道滚动模拟
///
/// 该方法用于创建滚动结束后的惯性滚动模拟,控制滚动的减速和回弹效果
///
/// 参数:
/// - [position]:当前滚动位置
/// - [velocity]:滚动速度
/// - 返回值:弹道滚动模拟对象,如果不需要模拟则返回null
Simulation? createBallisticSimulation(ScrollMetrics position, double velocity) {
// 延迟初始化视口渲染对象
viewportRender ??= findViewport(controller!.position?.context.storageContext);
// 检查是否启用了下拉刷新和上拉加载
final bool enablePullDown = viewportRender == null ? false : viewportRender!.firstChild is RenderSliverRefresh;
final bool enablePullUp = viewportRender == null ? false : viewportRender!.lastChild is RenderSliverLoading;
// 检查是否启用了对应方向的刷新
if ((velocity < 0.0 && !enablePullDown) || (velocity > 0 && !enablePullUp)) {
return parent!.createBallisticSimulation(position, velocity);
}
// 处理超出范围或二级刷新状态的情况
if (position.outOfRange) {
// 创建弹跳滚动模拟
return BouncingScrollSimulation(
spring: springDescription ?? spring,
position: position.pixels,
// 乘以0.91是为了避免弹簧回弹停止后释放手势
velocity: velocity * 0.91,
leadingExtent: position.minScrollExtent,
trailingExtent: position.maxScrollExtent,
tolerance: toleranceFor(position),
);
}
// 默认使用父级模拟
return super.createBallisticSimulation(position, velocity);
}