property_rent_vm.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import 'package:plugin_platform/http/http_result.dart';
  2. import 'package:riverpod_annotation/riverpod_annotation.dart';
  3. import 'package:shared/utils/log_utils.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:widgets/load_state_layout.dart';
  6. import 'package:widgets/widget_export.dart';
  7. import '../page/property_rent_state.dart';
  8. import '../repository/property_rent_repository.dart';
  9. part 'property_rent_vm.g.dart';
  10. @riverpod
  11. class PropertyRentVm extends _$PropertyRentVm {
  12. late PropertyRentRepository propertyRentRepository;
  13. bool _needShowPlaceholder = true; //是否展示LoadingView
  14. // Refresh 控制器
  15. final EasyRefreshController refreshController = EasyRefreshController(
  16. controlFinishRefresh: true, //允许刷新
  17. controlFinishLoad: true, //允许加载
  18. );
  19. PropertyRentState initState() {
  20. return PropertyRentState(
  21. list: [],
  22. );
  23. }
  24. @override
  25. PropertyRentState build() {
  26. // 引入数据仓库
  27. propertyRentRepository = ref.read(propertyRentRepositoryProvider);
  28. // 初始化状态
  29. PropertyRentState state = initState();
  30. // 初始化列表数据
  31. return state;
  32. }
  33. //刷新页面状态
  34. void changeLoadingState(LoadState loadState, String? errorMsg) {
  35. state = state.copyWith(
  36. loadingState: loadState,
  37. errorMessage: errorMsg
  38. );
  39. }
  40. // 初始化页面数据
  41. initPageData() {
  42. Log.d("----property_news_vm-----initPageData");
  43. onRefresh();
  44. }
  45. // 上拉加载 更多
  46. Future loadMore() async {
  47. Log.d("----property_news_vm-----loadMore");
  48. // await Future.delayed(const Duration(seconds: 2));
  49. // if(state.list.length >= state.filterCount){
  50. // return;
  51. // }else {
  52. // int curPage = state.curPage + 1;
  53. // state = state.copyWith(curPage: curPage,);
  54. // getListData();
  55. // }
  56. // 检查 curPage 是否为 null,并初始化为 1
  57. int newCurPage = state.curPage ?? 1;
  58. state = state.copyWith(curPage: ++newCurPage);
  59. getListData();
  60. }
  61. // 下拉刷新
  62. Future onRefresh() async {
  63. Log.d("----property_news_vm-----onRefresh ");
  64. // await Future.delayed(const Duration(seconds: 2));
  65. state = state.copyWith(curPage: 1);
  66. getListData();
  67. }
  68. // 重试请求
  69. Future retryRequest() async {
  70. state = state.copyWith(curPage: 1);
  71. _needShowPlaceholder = true;
  72. getListData();
  73. }
  74. // 获取list 列表数据
  75. Future getListData<T>() async {
  76. if (_needShowPlaceholder) {
  77. changeLoadingState(LoadState.State_Loading, null);
  78. }
  79. Log.d("加载listData数据---------------start--${state.curPage}---");
  80. // try {
  81. // //请求网络
  82. // Map<String, dynamic> params = {
  83. // "curPage": state.curPage,
  84. // "pageSize": state.pageSize,
  85. // };
  86. // Log.d("请求参数------$params");
  87. // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
  88. // Log.d("请求完成结果------${result.data}");
  89. // //校验成功失败
  90. // if (result.isSuccess) {
  91. // // state = state.copyWith(serverTime: result.data);
  92. // state = state;
  93. // handleList(listResult.data?.rows);
  94. // ToastEngine.show("获取数据成功");
  95. // } else {
  96. // errorMessage = listResult.errorMsg;
  97. // changeLoadingState(LoadState.State_Error);
  98. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  99. // }
  100. // } catch (e) {
  101. // ToastEngine.show("Error: $e");
  102. // }
  103. await Future.delayed(const Duration(milliseconds: 1500));
  104. final List<Map<String, dynamic>> listData = [
  105. {
  106. "id": 1,
  107. "title": "Jul 2024 Blk XX #XX to XX 1,100 - 1,200 sqft",
  108. "price": "\$4000",
  109. "unit": "per month",
  110. },
  111. {
  112. "id": 2,
  113. "title": "Jul 2024 Blk XX #XX to XX 1,100 - 1,200 sqft",
  114. "price": "\$4000",
  115. "unit": "per month",
  116. },
  117. ];
  118. if (state.curPage == 1) {
  119. //刷新的方式
  120. state = state.copyWith(list: listData);
  121. refreshController.finishRefresh();
  122. //更新展示的状态
  123. changeLoadingState(LoadState.State_Success, null);
  124. } else {
  125. //加载更多
  126. final allList = state.list;
  127. allList.addAll(listData);
  128. refreshController.finishLoad();
  129. state = state.copyWith(list: allList);
  130. }
  131. // 最后赋值
  132. _needShowPlaceholder = false;
  133. }
  134. }