YSRefrehLoad.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:flutter/material.dart';
  2. import 'package:pull_to_refresh/pull_to_refresh.dart';
  3. import 'YSNetWork.dart';
  4. import 'YSTools.dart';
  5. GlobalKey<YSRefreshLoadState> refreshKey = GlobalKey();
  6. GlobalKey<YSRefreshLoadState> refreshKey2 = GlobalKey();
  7. GlobalKey<YSRefreshLoadState> refreshKey3 = GlobalKey();
  8. GlobalKey<YSRefreshLoadState> refreshKey4 = GlobalKey();
  9. class YSRefreshLoad extends StatefulWidget {
  10. final Widget child;
  11. final ValueSetter postData;
  12. final String url;
  13. final Map request;
  14. const YSRefreshLoad({Key? key, required this.url, required this.request, required this.child, required this.postData,}) : super(key: key);
  15. @override
  16. YSRefreshLoadState createState() => YSRefreshLoadState();
  17. }
  18. class YSRefreshLoadState extends State<YSRefreshLoad> {
  19. int _page = 1;
  20. final RefreshController _refreshController = RefreshController(initialRefresh: false);
  21. List _dataArray = [];
  22. int _totalNum = 0;
  23. bool _isNull = true;
  24. Map _searchRequest = {};
  25. @override
  26. void initState() {
  27. Future.delayed(const Duration(seconds: 0)).then((value){
  28. _refreshData();
  29. });
  30. super.initState();
  31. }
  32. refresh({Map? request}){
  33. if(request!=null){
  34. _searchRequest = request;
  35. }
  36. _refreshData();
  37. }
  38. @override
  39. void dispose() {
  40. _refreshController.dispose();
  41. super.dispose();
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. return _isNull==true?Center(
  46. child: Column(
  47. mainAxisSize: MainAxisSize.min,
  48. children: [
  49. Image.asset('images/tzh_null.png',height: hsp(102),width: hsp(175),),
  50. Text('\n暂无内容\n\n\n\n\n\n\n\n',style: TextStyle(fontSize: zsp(14),color: Color(0xFFC8C9CC)),)
  51. ],
  52. ),
  53. ):SmartRefresher(
  54. onRefresh: _refreshData,
  55. onLoading: _loadMore,
  56. enablePullUp: _dataArray.length<_totalNum,
  57. controller: _refreshController,
  58. header: const ClassicHeader(
  59. height: 45.0,
  60. releaseText: '松开手刷新',
  61. refreshingText: '刷新中',
  62. completeText: '刷新完成',
  63. failedText: '刷新失败',
  64. idleText: '下拉刷新',
  65. ),
  66. footer: const ClassicFooter(
  67. height: 45.0,
  68. canLoadingText: '没有更多数据了',
  69. noDataText: ' 无数据',
  70. loadingText: '加载中 ',
  71. failedText: '加载失败',
  72. idleText: '上拉加载',
  73. ),
  74. child: NotificationListener<CustomNotification>(
  75. onNotification: (value){
  76. _refreshData();
  77. return true;
  78. },
  79. child: _dataArray.isEmpty?Center(
  80. child: Column(
  81. mainAxisSize: MainAxisSize.min,
  82. children: [
  83. Image.asset('images/tzh_null.png',height: hsp(102),width: hsp(175),),
  84. Text('\n暂无内容\n\n\n\n\n\n\n\n',style: TextStyle(fontSize: zsp(14),color: const Color(0xFFC8C9CC)),)
  85. ],
  86. ),
  87. ):widget.child,
  88. )
  89. );
  90. }
  91. Future<void> _refreshData() async{
  92. _page = 1;
  93. Map request = {'current':_page,'size':'10'};
  94. request.addAll(widget.request);
  95. if(_searchRequest.isNotEmpty)request.addAll(_searchRequest);
  96. YSNetWork.ysRequestHttp(context, type: RequestType.get, api: widget.url, parameter: request, successSetter: (dict){
  97. _isNull = false;
  98. Map data = dict['data']?? {};
  99. _totalNum = data['total']??0;
  100. _dataArray = data['records']??[];
  101. _refreshController.refreshCompleted();
  102. widget.postData(_dataArray);
  103. },isLoading: true);
  104. }
  105. Future<void> _loadMore() async{
  106. _page++;
  107. Map request = {'current':_page,'size':'10'};
  108. request.addAll(widget.request);
  109. if(_searchRequest.isNotEmpty)request.addAll(_searchRequest);
  110. YSNetWork.ysRequestHttp(context, type: RequestType.get, api: widget.url, parameter: request, successSetter: (dict){
  111. Map data = dict['data']?? {};
  112. _dataArray.addAll(data['records']??[]);
  113. _refreshController.loadComplete();
  114. widget.postData(_dataArray);
  115. },isLoading: true);
  116. }
  117. }
  118. class CustomNotification extends Notification {
  119. CustomNotification(this.value);
  120. final String value;
  121. }