YSRefrehLoad.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. class YSRefreshLoad extends StatefulWidget {
  9. final Widget child;
  10. final ValueSetter postData;
  11. final String url;
  12. final Map request;
  13. final bool isLoading;
  14. const YSRefreshLoad({Key? key, required this.url, required this.request, required this.child, required this.postData,this.isLoading = false,}) : 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: const 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?const 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. if(YSData().typeArray.isNotEmpty&&widget.url=='chains'){
  93. _isNull = false;
  94. _dataArray = YSData().typeArray;
  95. LogUtil.d(_dataArray);
  96. _refreshController.refreshCompleted();
  97. widget.postData(_dataArray);
  98. return;
  99. }
  100. _page = 1;
  101. Map request = {'page':1,'count':10};
  102. request.addAll(widget.request);
  103. if(_searchRequest.isNotEmpty)request.addAll(_searchRequest);
  104. YSNetWork.ysRequestHttp(context, type: RequestType.get, api: widget.url, parameter: request, successSetter: (dict){
  105. _isNull = false;
  106. Map data = dict['data']?? {};
  107. if(data['pages']!=null){
  108. _totalNum = data['pages']['total']??0;
  109. }
  110. _dataArray = data['list']??[];
  111. _refreshController.refreshCompleted();
  112. widget.postData(_dataArray);
  113. },isLoading: widget.isLoading,errorSetter: (){_refreshController.refreshCompleted();});
  114. }
  115. Future<void> _loadMore() async{
  116. _page++;
  117. Map request = {'page':1,'count':10};
  118. request.addAll(widget.request);
  119. if(_searchRequest.isNotEmpty)request.addAll(_searchRequest);
  120. YSNetWork.ysRequestHttp(context, type: RequestType.get, api: widget.url, parameter: request, successSetter: (dict){
  121. Map data = dict['data']?? {};
  122. _dataArray.addAll(data['list']??[]);
  123. _refreshController.loadComplete();
  124. widget.postData(_dataArray);
  125. },isLoading: widget.isLoading,errorSetter: (){_refreshController.refreshCompleted();});
  126. }
  127. }
  128. class CustomNotification extends Notification {
  129. CustomNotification(this.value);
  130. final String value;
  131. }