0


Flutter 项目实战 下拉刷新、上拉加载 九

/ 不同列表样式 /


普通列表下拉刷新与上拉加载


GridView 列表下拉刷新和上拉加载

​​​​

/ 自定义下拉刷新、上拉加载样式 /

作为一名Flutter开发者 , 在选择依赖库的时候 需要注意的几个点 :

. 依赖库的评分 pull_to_refresh

. 依赖库最后更新的时间

. 依赖库问题的反馈情况

. 依赖库的来源

pull_to_refresh

自定义下拉刷新头部样式

  1. ClassicHeader classicHeader() {
  2. return ClassicHeader(
  3. releaseText: '松开手刷新',
  4. refreshingText: '刷新中',
  5. completeText: '刷新完成',
  6. failedText: '刷新失败',
  7. idleText: '下拉刷新',
  8. completeIcon: const Icon(Icons.done, color: Colors.red),
  9. idleIcon: const Icon(Icons.arrow_downward, color: Colors.red),
  10. releaseIcon: const Icon(Icons.refresh, color: Colors.red),
  11. failedIcon: const Icon(Icons.error, color: Colors.red),
  12. refreshingIcon: cirProInt(),
  13. );
  14. }

旋转动画

  1. Container cirProInt() {
  2. return Container(
  3. child: CircularProgressIndicator(
  4. valueColor: AlwaysStoppedAnimation(Colors.red),
  5. strokeWidth: 4.0,
  6. backgroundColor: Colors.blue,
  7. ),
  8. width: 20.0,
  9. height: 20.0,
  10. );
  11. }

自定义上拉加载更多底部样式

. 简单定义

  1. ClassicFooter classicFooter() {
  2. return ClassicFooter(
  3. height: 100.0,
  4. loadingText: '加载中...',
  5. noDataText: '暂无数据',
  6. failedText: '加载失败',
  7. idleText: '上拉加载',
  8. );
  9. }

. 自定义上拉加载

根据不同的加载状态显示不同的提示 ; 设置底部自定义视图点击可以加载更多数据 .

  1. CustomFooter? customerFooter(State state, RefreshController controller,
  2. {ILoadDataCallBack? callBack}) {
  3. return CustomFooter(
  4. height: 40.0,
  5. builder: (BuildContext context, LoadStatus? mode) {
  6. ///上拉加载提示
  7. String loadTips = '';
  8. ///是否加载中
  9. bool isLoading = false;
  10. ///是否可点击加载更多
  11. bool isCanClick = false;
  12. if (mode == LoadStatus.idle) {
  13. loadTips = '上拉加载更多数据';
  14. isLoading = false;
  15. isCanClick = false;
  16. } else if (mode == LoadStatus.loading) {
  17. isLoading = true;
  18. isCanClick = false;
  19. loadTips = '数据加载中...';
  20. } else if (mode == LoadStatus.failed) {
  21. loadTips = '加载失败,上拉加载更多数据';
  22. isCanClick = false;
  23. isLoading = false;
  24. } else if (mode == LoadStatus.canLoading) {
  25. loadTips = '点击加载更多数据';
  26. isLoading = false;
  27. isCanClick = true;
  28. } else {
  29. isLoading = false;
  30. loadTips = '暂无数据,点击可重新加载数据';
  31. isCanClick = true;
  32. }
  33. return GestureDetector(
  34. onTap: () {
  35. if (isCanClick) {
  36. controller.footerMode!.value = LoadStatus.canLoading;
  37. isLoading = true;
  38. print('上拉加载更多 $isCanClick');
  39. }
  40. },
  41. child: Container(
  42. height: 40.0,
  43. color: Colors.transparent,
  44. child: Center(
  45. child: isLoading
  46. ? Row(
  47. mainAxisAlignment: MainAxisAlignment.center,
  48. children: [
  49. cirProInt(),
  50. Text(
  51. '\t\t$loadTips',
  52. style: TextStyle(
  53. color: Color(0xFFBDBDBD), fontSize: 14.0),
  54. ),
  55. ],
  56. )
  57. : Text(
  58. '$loadTips',
  59. style:
  60. TextStyle(color: Color(0xFFBDBDBD), fontSize: 14.0),
  61. ),
  62. ),
  63. ));
  64. },
  65. loadStyle: LoadStyle.ShowAlways,
  66. );
  67. }
  68. ///加载更多数据
  69. typedef void ILoadDataCallBack();

. 使用自定义加载更多 customerFooter

/ 列表滚动是否可优化 /

通过开发者工具 AS 查看 Frame rendering times (大概意思就是每秒视图渲染多少帧)

. 理想状态下每秒渲染的帧数是接近1秒钟60帧 ,下面的状态是比1秒钟60帧低了很多 . 列表滚动的过程中需要渲染的列表的每个视图 视图里面包含了一张图片 . 可以尝试不加载图片 , 观察一下每一秒绘制多少帧 .


列表每一项

. 取消图片加载 ,观察列表滚动时 每秒钟渲染的帧数 , 有明显的上升趋势 . 所以为了进一步优化, 我们需要在滚动列表时 , 列表停止滚动加载视图 .


列表每一项

. 监听列表滚动状态 ( NotificationListener )


列表滚动状态

  1. bool notificationFunction(Notification notification) {
  2. ///通知类型
  3. switch (notification.runtimeType) {
  4. case ScrollStartNotification:
  5. print("开始滚动");
  6. ///刷新页面 不加载图片
  7. isLoadingPic = false;
  8. break;
  9. case ScrollUpdateNotification:
  10. print("正在滚动");
  11. break;
  12. case ScrollEndNotification:
  13. print("滚动停止");
  14. ///刷新页面 加载图片
  15. setState(() {
  16. isLoadingPic = true;
  17. });
  18. break;
  19. case OverscrollNotification:
  20. print("滚动到边界");
  21. break;
  22. }
  23. return true;
  24. }

. 列表滚动过程中不渲染图片到列表, 滚动完成渲染 并查看每秒绘制多少帧 到情况 .


通过滚动状态控制图片的加载

免费接口 京东云

下拉刷新、上拉加载更多案例

标签: flutter

本文转载自: https://blog.csdn.net/u013491829/article/details/122308775
版权归原作者 xmiaoshen 所有, 如有侵权,请联系我们删除。

“Flutter 项目实战 下拉刷新、上拉加载 九”的评论:

还没有评论