当前位置: 首页 > news >正文

flutter实现上拉到底部加载更多数据

实现上拉加载数据,效果如下:

flutter滚动列表加载数据

使用的库主要是infinite_scroll_pagination ,

安装请查看官网

接口用的是https://reqres.in/提供的接口

请求接口用到的库是dio

下面主要是介绍如何使用infinite_scroll_pagination实现上拉加载数据,详细学习其它例子请查看infinite_scroll_pagination

main.dart全部代码如下

// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';import 'package:dio/dio.dart' as MyDio;void main() => runApp(const MyApp());class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Infinite Scroll Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: const InfiniteScrollList(),);}
}class InfiniteScrollList extends StatefulWidget {const InfiniteScrollList({super.key});@override// ignore: library_private_types_in_public_api_InfiniteScrollListState createState() => _InfiniteScrollListState();
}class _InfiniteScrollListState extends State<InfiniteScrollList> {final PagingController<int, UserData> _listController =PagingController(firstPageKey: 1);@overridevoid initState() {super.initState();_listController.addPageRequestListener((page) {_fetchListData(page);});}Future<void> _fetchListData(int page) async {MyDio.Dio dio = MyDio.Dio();MyDio.Response response = await dio.get('https://reqres.in/api/users',queryParameters: {'page': page,// 'stage_id': 4,// 'category_id': 3,});ApiResponse data = ApiResponse.fromJson(response.data);if (data.data.isEmpty) {_listController.appendLastPage(data.data);} else {final nextPageKey = page + 1;_listController.appendPage(data.data, nextPageKey);}}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('Infinite Scroll Demo')),body: PagedListView<int, UserData>(pagingController: _listController,builderDelegate: PagedChildBuilderDelegate<UserData>(itemBuilder: (context, item, index) =>ListTile(title: SizedBox(child: Text(item.firstName))),noMoreItemsIndicatorBuilder: (context) => const Center(child: Padding(padding: EdgeInsets.all(20.0),child: Text('没有更多了'),),),),),);}@overridevoid dispose() {_listController.dispose();super.dispose();}
}class ApiResponse {final int page;final int perPage;final int total;final int totalPages;final List<UserData> data;final Support support;ApiResponse({required this.page, required this.perPage, required this.total, required this.totalPages, required this.data, required this.support});factory ApiResponse.fromJson(Map<String, dynamic> json) {var list = json['data'] as List;List<UserData> dataList = list.map((i) => UserData.fromJson(i)).toList();return ApiResponse(page: json['page'],perPage: json['per_page'],total: json['total'],totalPages: json['total_pages'],data: dataList,support: Support.fromJson(json['support']),);}
}class UserData {final int id;final String email;final String firstName;final String lastName;final String avatar;UserData({required this.id, required this.email, required this.firstName, required this.lastName, required this.avatar});factory UserData.fromJson(Map<String, dynamic> json) {return UserData(id: json['id'],email: json['email'],firstName: json['first_name'],lastName: json['last_name'],avatar: json['avatar'],);}
}class Support {final String url;final String text;Support({required this.url, required this.text});factory Support.fromJson(Map<String, dynamic> json) {return Support(url: json['url'],text: json['text'],);}
}

http://www.lryc.cn/news/221337.html

相关文章:

  • UE4 Niagara Module Script 初次使用笔记
  • 【Spring Boot 源码学习】JedisConnectionConfiguration 详解
  • 联想服务器-HTTP boot安装Linux系统
  • 容器滚动更新过程中流量无损
  • 深入理解JS中的this
  • rust 基础数据类型
  • ELK极简上手
  • 在 JavaScript 中,变量的作用域是如何确定的?
  • 常见面试题-TCP三次握手四次挥手
  • 前端框架Vue学习 ——(六)Vue组件库Element
  • 第六章:Property-based Testing and Test Oracles
  • react生命周期函数
  • QSqlDatabase使用Sqlite
  • 宝马——使用人工智能制造和驾驶汽车
  • java入门,Map<? extends String, ?>
  • Spring Boot 统一处理功能
  • 香港金融科技周VERTU CSO Sophie谈Web3.0的下一个风口 手机虚拟货币移动支付
  • 分布式单元化
  • wvp-gb28181-pro接入海康摄像头
  • 近视眼选择什么台灯好?专家推荐的防近视台灯
  • 数据标注工具【LabelImg】安装使用 用VOC制作自己的数据集
  • Zeus IoT : 基于 SpringBoot 的分布式开源物联网大数据平台
  • 面试—如何介绍项目中的多级缓存?
  • PyTorch入门学习(十七):完整的模型训练套路
  • 《 Hello 算法 》 - 免费开源的数据结构与算法入门教程电子书,包含大量动画、图解,通俗易懂
  • 数据库之事务
  • NOIP2023模拟12联测33 B. 游戏
  • 代码随想录打卡第六十三天|84.柱状图中最大的矩形
  • python tempfile 模块使用
  • 【软件测试】接口测试实战详解