rewards_address_vm.dart 3.2 KB

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