flutter开发实战-下拉刷新与上拉加载更多实现

news/2025/2/21 7:07:59/

flutter开发实战-下拉刷新与上拉加载更多实现

在开发中经常遇到列表需要下拉刷新与上拉加载更多,这里使用EasyRefresh,版本是3.3.2+1

一、什么是EasyRefresh

EasyRefresh可以在Flutter应用程序上轻松实现下拉刷新和上拉加载。它几乎支持所有Flutter Scrollable小部件。它的功能与安卓的SmartRefreshLayout非常相似,也吸收了许多第三方库的优势。EasyRefresh集成了各种风格的页眉和页脚,但它没有任何限制,您可以轻松自定义。使用Flutter强大的动画,即使只是一个简单的控制也可以完成。EasyRefresh的目标是为Flutter创建一个强大、稳定、成熟的pull-to-refresh框架。

二、实现下拉刷新与上拉加载更多

在pubspec.yaml中引入EasyRefresh

 # 下拉刷新、上拉更多easy_refresh: ^3.3.2+1

在使用EasyRefresh过程中,需要用到EasyRefreshController来控制刷新结束。

/// Finish the refresh task and return the result./// [result] Result of task completion./// [force] Enforced, used to modify the result.void finishRefresh([IndicatorResult result = IndicatorResult.success, bool force = false]) {assert(controlFinishRefresh || force,'Please set controlFinishRefresh to true, then use. If you want to modify the result, you can set force to true.');_state?._headerNotifier._finishTask(result);}/// Finish the load task and return the result./// [result] Result of task completion./// [force] Enforced, used to modify the result.void finishLoad([IndicatorResult result = IndicatorResult.success, bool force = false]) {assert(controlFinishLoad || force,'Please set controlFinishLoad to true, then use. If you want to modify the result, you can set force to true.');_state?._footerNotifier._finishTask(result);}

整体实现下拉刷新与上拉加载更多完整代码如下

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';class RefreshPage extends StatefulWidget {const RefreshPage({super.key});State<RefreshPage> createState() => _RefreshPageState();
}class _RefreshPageState extends State<RefreshPage> {int _count = 10;late EasyRefreshController _controller;void initState() {super.initState();_controller = EasyRefreshController(controlFinishRefresh: true,controlFinishLoad: true,);}void dispose() {_controller.dispose();super.dispose();}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('EasyRefresh'),),body: EasyRefresh(controller: _controller,header: const BezierCircleHeader(),footer: const ClassicFooter(),onRefresh: () async {await Future.delayed(const Duration(seconds: 4));if (!mounted) {return;}setState(() {_count = 10;});_controller.finishRefresh();_controller.resetFooter();},onLoad: () async {await Future.delayed(const Duration(seconds: 4));if (!mounted) {return;}setState(() {_count += 5;});_controller.finishLoad(_count >= 20 ? IndicatorResult.noMore : IndicatorResult.success);},child: ListView.builder(itemBuilder: (context, index) {return Card(child: Container(alignment: Alignment.center,height: 80,child: Text('${index + 1}'),),);},itemCount: _count,),),);}
}

整体效果图如下

在这里插入图片描述

三、实现下拉刷新与上拉加载更多

flutter开发实战-下拉刷新与上拉加载更多实现。

https://blog.csdn.net/gloryFlow/article/details/133869961

学习记录,每天不停进步。


http://www.ppmy.cn/news/1158986.html

相关文章

计网面试复习自用

五层&#xff1a; 应用层&#xff1a;应用层是最高层&#xff0c;负责为用户提供网络服务和应用程序。在应用层&#xff0c;用户应用程序与网络进行交互&#xff0c;发送和接收数据。典型的应用层协议包括HTTP&#xff08;用于网页浏览&#xff09;、SMTP&#xff08;用于电子邮…

游戏缺少dll文件用什么修复?dll多种修复方法指南

在玩游戏时&#xff0c;有时候可能会遇到游戏缺少dll文件的问题。dll文件是动态链接库的缩写&#xff0c;它包含了一些函数和资源&#xff0c;游戏运行需要依赖这些文件。如果缺少了某个dll文件&#xff0c;游戏就可能无法正常运行。那么游戏缺少dll文件用什么修复&#xff1f;…

关于React

当今的Web开发世界中&#xff0c;React已经成为前端开发的主要工具之一。它的强大和灵活性使开发人员能够构建复杂的用户界面&#xff0c;同时保持代码的可维护性。本篇博客文章将深入探讨React&#xff0c;包括其核心概念、组件化开发、状态管理、性能优化和生态系统。 1. Re…

C++产生未定义的行为的原因分析

前言 最近一直在做QT开发&#xff0c;编程环境是VS2017和QT5.11.2 经常遇到的问题就是&#xff0c;在VS中调试程序&#xff0c;前面都是正常运行的&#xff0c;但是当关闭窗口&#xff0c;退出程序的时候&#xff0c;VS会抛出一个异常 “未加载ntdll.pdb&#xff0c;触发了一…

videojs和videojs-markers

文章目录 videojs安装使用videojs常用选项video.js特定选项 videojs-markers安装使用说明方法 videojs video.js是一款基于HTML5的网络视频播放器。它支持HTML5和Flash视频&#xff0c;以及YouTube和Vimeo&#xff08;通过插件&#xff09;&#xff0c;Video.js 自动检测浏览器…

解决Dev C++编译或运行报错 Source file not compiled

最近在研究青少年编程&#xff0c;用到DevC&#xff0c;写了个程序点击编译并运行后&#xff0c;我得到了一个错误消息&#xff1a;Source file not compiled。网上查了一下&#xff1a;原因是bloodshed Dev C与Windows10或者11不兼容所以才会报&#xff1a;Source file not co…

uniapp-vue3-微信小程序-按钮组wo-btn-group

采用uniapp-vue3实现, 是一款支持高度自定义的按钮组组件&#xff0c;支持H5、微信小程序&#xff08;其他小程序未测试过&#xff0c;可自行尝试&#xff09; 可到插件市场下载尝试&#xff1a; https://ext.dcloud.net.cn/plugin?id15012 使用示例 <template><vie…

“探寻服务器的无限潜能:从创意项目到在线社区,你会做什么?”

文章目录 每日一句正能量前言什么是服务器&#xff1f;服务器能做什么&#xff1f;服务器怎么用&#xff1f;部署创意项目&#xff0c;还是在线社区亦或做其他的&#xff1f;后记 每日一句正能量 未知的下一秒&#xff0c;千万不要轻言放弃。 前言 在数字化时代&#xff0c;服…