rewards_home_vm.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:plugin_platform/http/http_result.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  7. import 'package:widgets/picker/option_pick_util.dart';
  8. import './rewards_home_state.dart';
  9. import './rewards_home_repository.dart';
  10. part 'rewards_home_vm.g.dart';
  11. @riverpod
  12. class RewardsHomeVm extends _$RewardsHomeVm {
  13. late RewardsHomeRepository rewardsHomeRepository;
  14. RewardsHomeState initState() {
  15. return RewardsHomeState(
  16. curPage: 1,
  17. pageSize: 10,
  18. list: [
  19. {
  20. "id": 1,
  21. "title":
  22. "The community will hold the activity of making Zongzi on the Loong Boat ……",
  23. "price": "Monday 14 0ct 2024, 15:00 PM~18:00PM",
  24. },
  25. {
  26. "id": 2,
  27. "title": "Community basketball competition activities",
  28. "price": "Monday 14 Oct 2024, 10:19 AM",
  29. },
  30. ],
  31. filterCount: 2,
  32. lists: [
  33. {'title': 'YY', 'icon': Assets.rewardsRewardsIndex1},
  34. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex2},
  35. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex3},
  36. {'title': 'Beauty', 'icon': Assets.rewardsRewardsIndex4},
  37. {'title': 'Activites', 'icon': Assets.rewardsRewardsIndex5},
  38. {'title': 'Travel', 'icon': Assets.rewardsRewardsIndex6},
  39. {'title': 'Electronics', 'icon': Assets.rewardsRewardsIndex7},
  40. {'title': 'Gifts', 'icon': Assets.rewardsRewardsIndex8},
  41. {'title': 'Services', 'icon': Assets.rewardsRewardsIndex9},
  42. ],
  43. );
  44. }
  45. @override
  46. RewardsHomeState build() {
  47. // 初始化列表数据
  48. // 初始化列表数据
  49. // 引入数据仓库
  50. Log.d("----property_news_vm-----initPageData");
  51. rewardsHomeRepository = ref.read(rewardsHomeRepositoryProvider);
  52. // 初始化状态
  53. RewardsHomeState state = initState();
  54. // 初始化列表数据
  55. return state;
  56. }
  57. // 初始化页面数据
  58. initPageData() {
  59. Log.d("----property_news_vm-----initPageData");
  60. refreshListData();
  61. }
  62. // 上拉加载
  63. Future onLoadData() async {
  64. Log.d("----property_news_vm-----initListData");
  65. // await Future.delayed(const Duration(seconds: 2));
  66. // if(state.list.length >= state.filterCount)Home{
  67. // return;
  68. // }else {
  69. // int curPage = state.curPage + 1;
  70. // state = state.copyWith(curPage: curPage,);
  71. // getListData();
  72. // }
  73. getListData();
  74. }
  75. // 去新闻详情页
  76. void goNewsDetail(String item) {
  77. Log.d(item);
  78. // PropertyPage.startInstance(context: context, item: item);
  79. }
  80. // 获取list 列表数据
  81. void getListData<T>() async {
  82. Log.d("加载listData数据---------------start-----");
  83. try {
  84. //请求网络
  85. Map<String, dynamic> params = {
  86. "curPage": state.curPage,
  87. "pageSize": state.pageSize,
  88. };
  89. Log.d("请求参数------$params");
  90. final result = await rewardsHomeRepository.fetchPropertyNewsList(params);
  91. Log.d("请求完成结果------${result.data}");
  92. //校验成功失败
  93. if (result.isSuccess) {
  94. // state = state.copyWith(serverTime: result.data);
  95. state = state;
  96. ToastEngine.show("获取数据成功");
  97. } else {
  98. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  99. }
  100. } catch (e) {
  101. ToastEngine.show("Error: $e");
  102. }
  103. }
  104. // 下拉刷新
  105. Future refreshListData() async {
  106. Log.d("----property_news_vm-----refreshListData ");
  107. // await Future.delayed(const Duration(seconds: 2));
  108. state = state.copyWith(curPage: 1, pageSize: 10);
  109. // ref.invalidateSelf();
  110. // ref.invalidate(propertyNewsVmProvider);
  111. getListData();
  112. }
  113. }