Android Flutter實現(xiàn)上拉加載組件的示例代碼
前言
在此之前對列表下拉刷新做了調(diào)整方案,具體介紹可以閱讀下拉刷新組件交互調(diào)整。既然列表有下拉刷新外當然還有上拉加載更多操作了,本次就來介紹如何為列表增加上拉加載更多的交互實現(xiàn)。
實現(xiàn)方案
上拉刷新實現(xiàn)形式上可以有多種實現(xiàn)方式,若不帶交互形式可采用NotificationListener組件監(jiān)聽滑動來實現(xiàn)上拉加載更多;如果對操作交互上有一定要求期望上拉刷新帶有直觀動畫可操作性就需要實現(xiàn)一定樣式來實現(xiàn)了。
監(jiān)聽NotificationListener實現(xiàn)
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調(diào)整

BouncingScrollPhysics是iOS帶有阻尼效果滑動交互,在下拉刷新中帶有回彈阻尼效果是比較好的交互,但在上拉加載更多獲取交互上這種效果或許有點多余。因此需要定制下拉刷新帶有回彈阻尼效果,上拉加載沒有回彈阻尼效果的。ScrollPhysics
CustomScrollView( physics: BouncingScrollPhysics(), slivers: <Widget>[] )
具體實現(xiàn)代碼如下所示:
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);
/// 阻尼參數(shù)計算
@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;
}但是直接會發(fā)生錯誤日志,由于回調(diào)中OverscrollIndicatorNotification是沒有metrics對象。對于
The following NoSuchMethodError was thrown while notifying listeners for AnimationController: Class 'OverscrollIndicatorNotification' has no instance getter 'metrics'.
由于上滑沒有阻尼滑動到底部回調(diào)Notification發(fā)生了變化,因此需要在NotificationListener中增加判斷對超出滑動范圍回調(diào)進行過濾處理避免異常情況發(fā)生。
if(scrollNotification is OverscrollIndicatorNotification){
return false;
}結果展示

以上就是Android Flutter實現(xiàn)上拉加載組件的示例代碼的詳細內(nèi)容,更多關于Android Flutter上拉加載的資料請關注腳本之家其它相關文章!
相關文章
AndroidStudio集成OpenCV的實現(xiàn)教程
本文主要介紹了Android?Studio集成OpenCV的實現(xiàn)教程,文中通過圖文介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
android編程實現(xiàn)局部界面動態(tài)切換的方法
這篇文章主要介紹了android編程實現(xiàn)局部界面動態(tài)切換的方法,以實例形式較為詳細的分析了Android局部切換的布局及功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

