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


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

    flutter滚动列表加载数据

    使用的库主要是infinite_scroll_pagination ,

    安装请查看官网

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

    请求接口用到的库是dio

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

    main.dart全部代码如下

    1. // ignore_for_file: non_constant_identifier_names
    2. import 'package:flutter/material.dart';
    3. import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
    4. import 'package:dio/dio.dart' as MyDio;
    5. void main() => runApp(const MyApp());
    6. class MyApp extends StatelessWidget {
    7. const MyApp({super.key});
    8. @override
    9. Widget build(BuildContext context) {
    10. return MaterialApp(
    11. title: 'Infinite Scroll Demo',
    12. theme: ThemeData(
    13. primarySwatch: Colors.blue,
    14. ),
    15. home: const InfiniteScrollList(),
    16. );
    17. }
    18. }
    19. class InfiniteScrollList extends StatefulWidget {
    20. const InfiniteScrollList({super.key});
    21. @override
    22. // ignore: library_private_types_in_public_api
    23. _InfiniteScrollListState createState() => _InfiniteScrollListState();
    24. }
    25. class _InfiniteScrollListState extends State<InfiniteScrollList> {
    26. final PagingController<int, UserData> _listController =
    27. PagingController(firstPageKey: 1);
    28. @override
    29. void initState() {
    30. super.initState();
    31. _listController.addPageRequestListener((page) {
    32. _fetchListData(page);
    33. });
    34. }
    35. Future<void> _fetchListData(int page) async {
    36. MyDio.Dio dio = MyDio.Dio();
    37. MyDio.Response response = await dio.get(
    38. 'https://reqres.in/api/users',
    39. queryParameters: {
    40. 'page': page,
    41. // 'stage_id': 4,
    42. // 'category_id': 3,
    43. });
    44. ApiResponse data = ApiResponse.fromJson(response.data);
    45. if (data.data.isEmpty) {
    46. _listController.appendLastPage(data.data);
    47. } else {
    48. final nextPageKey = page + 1;
    49. _listController.appendPage(data.data, nextPageKey);
    50. }
    51. }
    52. @override
    53. Widget build(BuildContext context) {
    54. return Scaffold(
    55. appBar: AppBar(title: const Text('Infinite Scroll Demo')),
    56. body: PagedListView<int, UserData>(
    57. pagingController: _listController,
    58. builderDelegate: PagedChildBuilderDelegate<UserData>(
    59. itemBuilder: (context, item, index) =>
    60. ListTile(title: SizedBox(child: Text(item.firstName))),
    61. noMoreItemsIndicatorBuilder: (context) => const Center(
    62. child: Padding(
    63. padding: EdgeInsets.all(20.0),
    64. child: Text('没有更多了'),
    65. ),
    66. ),
    67. ),
    68. ),
    69. );
    70. }
    71. @override
    72. void dispose() {
    73. _listController.dispose();
    74. super.dispose();
    75. }
    76. }
    77. class ApiResponse {
    78. final int page;
    79. final int perPage;
    80. final int total;
    81. final int totalPages;
    82. final List<UserData> data;
    83. final Support support;
    84. ApiResponse({required this.page, required this.perPage, required this.total, required this.totalPages, required this.data, required this.support});
    85. factory ApiResponse.fromJson(Map<String, dynamic> json) {
    86. var list = json['data'] as List;
    87. List<UserData> dataList = list.map((i) => UserData.fromJson(i)).toList();
    88. return ApiResponse(
    89. page: json['page'],
    90. perPage: json['per_page'],
    91. total: json['total'],
    92. totalPages: json['total_pages'],
    93. data: dataList,
    94. support: Support.fromJson(json['support']),
    95. );
    96. }
    97. }
    98. class UserData {
    99. final int id;
    100. final String email;
    101. final String firstName;
    102. final String lastName;
    103. final String avatar;
    104. UserData({required this.id, required this.email, required this.firstName, required this.lastName, required this.avatar});
    105. factory UserData.fromJson(Map<String, dynamic> json) {
    106. return UserData(
    107. id: json['id'],
    108. email: json['email'],
    109. firstName: json['first_name'],
    110. lastName: json['last_name'],
    111. avatar: json['avatar'],
    112. );
    113. }
    114. }
    115. class Support {
    116. final String url;
    117. final String text;
    118. Support({required this.url, required this.text});
    119. factory Support.fromJson(Map<String, dynamic> json) {
    120. return Support(
    121. url: json['url'],
    122. text: json['text'],
    123. );
    124. }
    125. }

  • 相关阅读:
    【MySQL】(1))连接方式+存储引擎+字符集
    scroll-view 实现滑动分类
    PLC控制一阶惯性纯滞后系统的大林(Dahlin)算法详解(SCL代码)
    【IJCAI】CostFormer即插即用的MVS高效代价体聚合Transformer,FaceChain团队出品
    vue的生命周期
    [附源码]计算机毕业设计springboot网上电影购票系统
    STC15单片机-低功耗设计
    杭州ALIENWARE外星人电脑(大悦城旗舰店),玩起来就是不一样
    一种关于低代码平台(LCDP)建设实践与设计思路
    [附源码]计算机毕业设计ssm新能源电动汽车充电桩服务APP
  • 原文地址:https://blog.csdn.net/Hhjian524/article/details/134251540