下拉刷新与上拉加载 - 鸿蒙Flutter无限滚动列表 概述下拉刷新和上拉加载是移动端应用中常见的交互模式用于实现数据的实时更新和分页加载。Flutter提供了RefreshIndicator组件实现下拉刷新通过ScrollController实现上拉加载更多。下拉刷新RefreshIndicatorRefreshIndicator构造函数RefreshIndicator({Key?key,requiredWidgetchild,// 子组件requiredRefreshCallbackonRefresh,// 刷新回调double displacement40,// 下拉距离Color?color,// 指示器颜色Color?backgroundColor,// 背景颜色double strokeWidth2.0,// 指示器宽度RefreshIndicatorModerefreshIndicatorLayoutDelegateconstDefaultRefreshIndicatorLayoutDelegate(),})基本用法RefreshIndicator(onRefresh:()async{// 执行刷新操作await_loadData();},child:ListView.builder(itemCount:items.length,itemBuilder:(context,index)ListTile(title:Text(items[index])),),)onRefresh回调onRefresh返回一个FutureRefreshIndicator会在Future完成后隐藏刷新指示器Futurevoid_onRefresh()async{// 模拟网络请求awaitFuture.delayed(constDuration(seconds:2));// 更新数据setState((){itemsList.generate(20,(i)刷新后的 Item$i);});}上拉加载更多使用ScrollController实现finalScrollController_controllerScrollController();bool _isLoadingfalse;int _page1;overridevoidinitState(){super.initState();_loadData();_controller.addListener(_scrollListener);}overridevoiddispose(){_controller.dispose();super.dispose();}void_scrollListener(){if(_controller.position.pixels_controller.position.maxScrollExtent-100){_loadMore();}}Futurevoid_loadData()async{setState(()_isLoadingtrue);awaitFuture.delayed(constDuration(seconds:1));setState((){itemsList.generate(20,(i)Item${(_page-1)*20i1});_isLoadingfalse;});}Futurevoid_loadMore()async{if(_isLoading)return;setState(()_isLoadingtrue);_page;awaitFuture.delayed(constDuration(seconds:1));setState((){items.addAll(List.generate(20,(i)Item${(_page-1)*20i1}));_isLoadingfalse;});}加载状态显示在列表末尾显示加载状态ListView.builder(controller:_controller,itemCount:items.length1,itemBuilder:(context,index){if(indexitems.length){return_isLoading?constPadding(padding:EdgeInsets.all(16),child:Center(child:CircularProgressIndicator()),):constPadding(padding:EdgeInsets.all(16),child:Center(child:Text(没有更多数据)),);}returnListTile(title:Text(items[index]));},)无限滚动列表实战完整实现classInfiniteScrollListextendsStatefulWidget{constInfiniteScrollList({super.key});overrideStateInfiniteScrollListcreateState()_InfiniteScrollListState();}class_InfiniteScrollListStateextendsStateInfiniteScrollList{finalScrollController_controllerScrollController();ListString_items[];bool _isLoadingfalse;bool _hasMoretrue;int _page1;overridevoidinitState(){super.initState();_loadData();_controller.addListener(_scrollListener);}overridevoiddispose(){_controller.dispose();super.dispose();}void_scrollListener(){if(!_hasMore||_isLoading)return;if(_controller.position.pixels_controller.position.maxScrollExtent-100){_loadMore();}}Futurevoid_loadData()async{setState(()_isLoadingtrue);awaitFuture.delayed(constDuration(seconds:1));setState((){_itemsList.generate(20,(i)Item${(_page-1)*20i1});_isLoadingfalse;_hasMoretrue;_page1;});}Futurevoid_loadMore()async{if(_isLoading||!_hasMore)return;setState(()_isLoadingtrue);_page;awaitFuture.delayed(constDuration(seconds:1));setState((){if(_page5){_hasMorefalse;}else{_items.addAll(List.generate(20,(i)Item${(_page-1)*20i1}));}_isLoadingfalse;});}overrideWidgetbuild(BuildContextcontext){returnRefreshIndicator(onRefresh:_loadData,child:ListView.builder(controller:_controller,itemCount:_items.length1,itemBuilder:(context,index){if(index_items.length){if(!_hasMore){returnconstPadding(padding:EdgeInsets.all(16),child:Center(child:Text(没有更多数据)),);}return_isLoading?constPadding(padding:EdgeInsets.all(16),child:Center(child:CircularProgressIndicator()),):constSizedBox.shrink();}returnListTile(title:Text(_items[index]));},),);}}自定义下拉刷新自定义RefreshIndicatorclassCustomRefreshIndicatorextendsStatefulWidget{finalWidgetchild;finalFuturevoidFunction()onRefresh;constCustomRefreshIndicator({super.key,requiredthis.child,requiredthis.onRefresh,});overrideStateCustomRefreshIndicatorcreateState()_CustomRefreshIndicatorState();}class_CustomRefreshIndicatorStateextendsStateCustomRefreshIndicator{bool _isRefreshingfalse;Futurevoid_handleRefresh()async{setState(()_isRefreshingtrue);awaitwidget.onRefresh();setState(()_isRefreshingfalse);}overrideWidgetbuild(BuildContextcontext){returnStack(children:[widget.child,if(_isRefreshing)constPositioned(top:0,left:0,right:0,child:LinearProgressIndicator(),),],);}}高级上拉加载使用NotificationListenerNotificationListenerScrollNotification(onNotification:(notification){if(notificationisScrollEndNotificationnotification.metrics.extentAfter100){_loadMore();}returnfalse;},child:ListView.builder(...),)使用第三方库// 推荐使用pull_to_refresh库importpackage:pull_to_refresh/pull_to_refresh.dart;SmartRefresher(controller:_refreshController,onRefresh:_onRefresh,onLoading:_onLoading,child:ListView.builder(...),)性能优化策略防抖处理避免频繁触发加载更多bool _isLoadingfalse;Timer?_debounceTimer;void_scrollListener(){if(_isLoading)return;if(_controller.position.pixels_controller.position.maxScrollExtent-100){_debounceTimer?.cancel();_debounceTimerTimer(constDuration(milliseconds:200),(){_loadMore();});}}预加载在滚动到距离底部一定距离时就开始加载void_scrollListener(){// 在距离底部200像素时开始预加载if(_controller.position.pixels_controller.position.maxScrollExtent-200){_loadMore();}}使用ValueNotifier避免频繁调用setStatefinalValueNotifierbool_isLoadingValueNotifier(false);void_loadMore()async{if(_isLoading.value)return;_isLoading.valuetrue;awaitFuture.delayed(constDuration(seconds:1));_isLoading.valuefalse;}ValueListenableBuilderbool(valueListenable:_isLoading,builder:(context,isLoading,child){returnisLoading?constCircularProgressIndicator():constSizedBox.shrink();},)下拉刷新与上拉加载对比特性下拉刷新上拉加载触发方式下拉手势滚动到底部用途更新当前数据加载更多数据实现方式RefreshIndicatorScrollController状态显示顶部指示器底部加载状态数据处理重置数据追加数据关键要点总结RefreshIndicator实现下拉刷新ScrollController实现上拉加载onRefresh返回Future完成后隐藏指示器监听滚动位置实现加载更多控制加载状态避免重复请求显示加载状态提升用户体验常见问题Q1: 下拉刷新不生效怎么办A: 确保子组件是可滚动的且高度超过屏幕。Q2: 上拉加载频繁触发怎么办A: 添加加载状态判断使用防抖处理。Q3: 如何自定义下拉刷新样式A: 使用RefreshIndicator的color和backgroundColor属性或自定义组件。Q4: 如何实现预加载A: 在距离底部一定距离时就开始加载。实践建议下拉刷新使用RefreshIndicator组件上拉加载使用ScrollController监听滚动位置加载状态在列表末尾显示加载状态防抖处理避免频繁触发加载资源释放在dispose中释放ScrollController复杂场景使用第三方库如pull_to_refresh通过合理实现下拉刷新和上拉加载可以创建出流畅的无限滚动列表提升用户体验。

相关新闻

最新新闻

火山云豆包大模型架构解析与企业级优化实践

火山云豆包大模型架构解析与企业级优化实践

1. 火山云豆包大模型的技术架构解析豆包大模型作为字节跳动旗下火山引擎推出的自研AI产品,其技术架构设计充分考虑了企业级应用场景的需求。从公开资料和行业实践来看,其核心架构采用分层设计理念,包含基础层、训练层、推理层和应用层四个关键…

2026/7/22 3:12:00
如何快速解决Cursor试用限制:完整的go-cursor-help工具指南

如何快速解决Cursor试用限制:完整的go-cursor-help工具指南

如何快速解决Cursor试用限制:完整的go-cursor-help工具指南 【免费下载链接】go-cursor-help 解决Cursor在免费订阅期间出现以下提示的问题: Your request has been blocked as our system has detected suspicious activity / Youve reached your trial request li…

2026/7/22 3:12:00
【2026最新】驾考科目一考试题库2309道电子版pdf

【2026最新】驾考科目一考试题库2309道电子版pdf

2026年机动车驾驶人科目一考试题库更新情况 2026年起,机动车驾驶人科目一考试题库已完成全面更新,新版题库试题总量为2309道。考试采用计算机随机组卷模式,每场考试从题库中随机抽取100道试题进行考核。 备考建议 全面覆盖学习 考生需系统…

2026/7/22 3:12:00
OpenCV核心API解析与性能优化实战

OpenCV核心API解析与性能优化实战

1. OpenCV核心API全景解析OpenCV作为计算机视觉领域的瑞士军刀,其API设计遵循"一次编写,到处运行"的跨平台理念。最新4.8.0版本包含超过2500个函数,主要分布在以下核心模块中:core:基础数据结构(…

2026/7/22 3:12:00
iptables服务

iptables服务

一、iptables介绍iptables服务是用户管理内核空间的iptables的管理工具,通过iptables书写内核空间的iptables策略iptables的规则是至上而下的读取方式,遇到与数据包信息匹配的规则后直接采用iptables的规则默认保存在内存中,如果需要永久保存…

2026/7/22 3:12:00
基于 STM32 的校园电动车充电桩智能计费与过载保护系统设计与实现

基于 STM32 的校园电动车充电桩智能计费与过载保护系统设计与实现

一、系统概述 本系统以 STM32 单片机为主控核心,针对校园电动车充电场景,设计一套集智能计费、过载保护、状态监测于一体的充电桩管理系统。系统支持刷卡计费、实时电流电压采集、过载自动断电、充电状态显示等功能,可有效解决校园电动车充电乱收费、过载起火等安全隐患问题…

2026/7/22 3:07:00

月新闻