property_sale_vm.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_sale_state.dart';
  8. import '../repository/property_sale_repository.dart';
  9. part 'property_sale_vm.g.dart';
  10. @riverpod
  11. class PropertySaleVm extends _$PropertySaleVm {
  12. late PropertySaleRepository propertySaleRepository;
  13. bool _needShowPlaceholder = true; //是否展示LoadingView
  14. // Refresh 控制器
  15. final EasyRefreshController refreshController = EasyRefreshController(
  16. controlFinishRefresh: true, //允许刷新
  17. controlFinishLoad: true, //允许加载
  18. );
  19. PropertySaleState initState() {
  20. return PropertySaleState(
  21. list: [],
  22. );
  23. }
  24. @override
  25. PropertySaleState build() {
  26. // 引入数据仓库
  27. propertySaleRepository = ref.read(propertySaleRepositoryProvider);
  28. // 初始化状态
  29. PropertySaleState 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": "18 Sep 2024 BIK 39#09-XX 1337 psd 1001 sqft",
  108. "price": "\$1.338 M",
  109. },
  110. {
  111. "id": 2,
  112. "title": "18 Sep 2024 BIK 39#09-XX 1337 psd 1001 sqft",
  113. "price": "\$1.338 M",
  114. },
  115. ];
  116. if (state.curPage == 1) {
  117. //刷新的方式
  118. state = state.copyWith(list: listData);
  119. refreshController.finishRefresh();
  120. //更新展示的状态
  121. changeLoadingState(LoadState.State_Success, null);
  122. } else {
  123. //加载更多
  124. final allList = state.list;
  125. allList.addAll(listData);
  126. refreshController.finishLoad();
  127. state = state.copyWith(list: allList);
  128. }
  129. // 最后赋值
  130. _needShowPlaceholder = false;
  131. }
  132. }