網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
在此之前對(duì)列表下拉刷新做了調(diào)整方案,具體介紹可以閱讀下拉刷新組件交互調(diào)整。既然列表有下拉刷新外當(dāng)然還有上拉加載更多操作了,本次就來(lái)介紹如何為列表增加上拉加載更多的交互實(shí)現(xiàn)。
實(shí)現(xiàn)方案
上拉刷新實(shí)現(xiàn)形式上可以有多種實(shí)現(xiàn)方式,若不帶交互形式可采用NotificationListener
組件監(jiān)聽(tīng)滑動(dòng)來(lái)實(shí)現(xiàn)上拉加載更多;如果對(duì)操作交互上有一定要求期望上拉刷新帶有直觀動(dòng)畫(huà)可操作性就需要實(shí)現(xiàn)一定樣式來(lái)實(shí)現(xiàn)了。
監(jiān)聽(tīng)NotificationListener實(shí)現(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 ? "加載更多...." : "沒(méi)有更多", style: TextStyle(fontSize: 25), ), ), )
ScrollPhysics調(diào)整
BouncingScrollPhysics
是iOS
帶有阻尼效果滑動(dòng)交互,在下拉刷新中帶有回彈阻尼效果是比較好的交互,但在上拉加載更多獲取交互上這種效果或許有點(diǎn)多余。因此需要定制下拉刷新帶有回彈阻尼效果,上拉加載沒(méi)有回彈阻尼效果的。ScrollPhysics
CustomScrollView( physics: BouncingScrollPhysics(), slivers: <Widget>[] )
具體實(shí)現(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ù)計(jì)算 @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; } /// 邊界條件 復(fù)用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; }
但是直接會(huì)發(fā)生錯(cuò)誤日志,由于回調(diào)中OverscrollIndicatorNotification
是沒(méi)有metrics
對(duì)象。對(duì)于
The following NoSuchMethodError was thrown while notifying listeners for AnimationController: Class 'OverscrollIndicatorNotification' has no instance getter 'metrics'.
由于上滑沒(méi)有阻尼滑動(dòng)到底部回調(diào)Notification
發(fā)生了變化,因此需要在NotificationListener
中增加判斷對(duì)超出滑動(dòng)范圍回調(diào)進(jìn)行過(guò)濾處理避免異常情況發(fā)生。
if(scrollNotification is OverscrollIndicatorNotification){ return false; }
結(jié)果展示
演示代碼看這里
原文鏈接:https://juejin.cn/post/7136948689628561444
相關(guān)推薦
- 2023-05-23 手把手教你如何一眼分辨是C還是C++_C 語(yǔ)言
- 2023-03-02 ijkPlayer播放器的全自動(dòng)編譯腳本及最終編譯包_Android
- 2022-05-25 pytorch?hook?鉤子函數(shù)的用法_python
- 2022-10-19 報(bào)錯(cuò)No?module?named?numpy問(wèn)題的解決辦法_python
- 2022-06-01 C語(yǔ)言?深入淺出講解指針的使用_C 語(yǔ)言
- 2023-07-29 koa2+sequelize中websocket的使用
- 2022-02-25 .gitignore 中增加了 .idea/ workspace.xml失效解決方案
- 2022-09-22 線程池的拒絕策略
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支