携手创作,共同生长!这是我参加「日新计划 8 月更文应战」的第6天,点击查看活动详情
前语
在此之前对列表下拉改写做了调整方案,详细介绍能够阅读下拉改写组件交互调整。既然列表有下拉改写外当然还有上拉加载更多操作了,本次就来介绍如何为列表添加上拉加载更多的交互完成。
完成方案
上拉改写完成形式上能够有多种完成方式,若不带交互形式可采用NotificationListener
组件监听滑动来完成上拉加载更多;假如对操作交互上有必定要求期望上拉改写带有直观动画可操作性就需要完成必定样式来完成了。
监听NotificationListener完成
NotificationListener(
onNotification: (scrollNotification) {
if (scrollNotification.metrics.pixels >=
scrollNotification.metrics.maxScrollExtent - size.height &&
scrollNotification.depth == 0) {
if (!isLoading) {
isLoading = true;
Future.delayed(Duration(seconds: 1), () {
isLoading = false;
length += 10;
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('下拉加载更多了!!!'),
duration: Duration(milliseconds: 700),
));
setState(() {});
});
}
}
return false;
}
......
)
NotificationListener添加样式
SliverToBoxAdapter(
child: Center(
child: Text(
length < 30 ? "加载更多...." : "没有更多",
style: TextStyle(fontSize: 25),
),
),
)
ScrollPhysics调整
BouncingScrollPhysics
是iOS
带有阻尼作用滑动交互,在下拉改写中带有回弹阻尼作用是比较好的交互,但在上拉加载更多获取交互上这种作用或许有点多余。因此需要定制下拉改写带有回弹阻尼作用,上拉加载没有回弹阻尼作用的。ScrollPhysics
CustomScrollView(
physics: BouncingScrollPhysics(),
slivers: <Widget>[]
)
详细完成代码如下所示:
class CustomBouncingScrollPhysics extends ScrollPhysics {
const CustomBouncingScrollPhysics({ ScrollPhysics parent }) : super(parent: parent);
@override
CustomBouncingScrollPhysics applyTo(ScrollPhysics ancestor) {
return CustomBouncingScrollPhysics(parent: buildParent(ancestor));
}
double frictionFactor(double overscrollFraction) => 0.52 * math.pow(1 - overscrollFraction, 2);
/// 阻尼参数计算
@override
double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
assert(offset != 0.0);
assert(position.minScrollExtent <= position.maxScrollExtent);
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);
final double overscrollPast = math.max(overscrollPastStart, overscrollPastEnd);
final bool easing = (overscrollPastStart > 0.0 && offset < 0.0)
|| (overscrollPastEnd > 0.0 && offset > 0.0);
final double friction = easing
// Apply less resistance when easing the overscroll vs tensioning.
? frictionFactor((overscrollPast - offset.abs()) / position.viewportDimension)
: frictionFactor(overscrollPast / position.viewportDimension);
final double direction = offset.sign;
return direction * _applyFriction(overscrollPast, offset.abs(), friction);
}
static double _applyFriction(double extentOutside, double absDelta, double gamma) {
assert(absDelta > 0);
double total = 0.0;
if (extentOutside > 0) {
final double deltaToLimit = extentOutside / gamma;
if (absDelta < deltaToLimit)
return absDelta * gamma;
total += extentOutside;
absDelta -= deltaToLimit;
}
return total + absDelta;
}
/// 鸿沟条件 复用ClampingScrollPhysics的方法 保留列表在底部的鸿沟判别条件
@override
double applyBoundaryConditions(ScrollMetrics position, double value){
if (position.maxScrollExtent <= position.pixels && position.pixels < value) // overscroll
return value - position.pixels;
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 (velocity.abs() >= tolerance.velocity || position.outOfRange) {
return BouncingScrollSimulation(
spring: spring,
position: position.pixels,
velocity: velocity * 0.91, // TODO(abarth): We should move this constant closer to the drag end.
leadingExtent: position.minScrollExtent,
trailingExtent: position.maxScrollExtent,
tolerance: tolerance,
);
}
return null;
}
@override
double get minFlingVelocity => kMinFlingVelocity * 2.0;
@override
double carriedMomentum(double existingVelocity) {
return existingVelocity.sign *
math.min(0.000816 * math.pow(existingVelocity.abs(), 1.967).toDouble(), 40000.0);
}
@override
double get dragStartDistanceMotionThreshold => 3.5;
}
可是直接会产生过错日志,因为回调中OverscrollIndicatorNotification
是没有metrics
对象。
The following NoSuchMethodError was thrown while notifying listeners for AnimationController:
Class 'OverscrollIndicatorNotification' has no instance getter 'metrics'.
对于上滑没有阻尼滑动到底部回调Notification
产生了改变,因此需要在NotificationListener
中添加判别对超出滑动规模回调进行过滤处理防止异常情况产生。
if(scrollNotification is OverscrollIndicatorNotification){
return false;
}
结果展现
演示代码看这儿