rewards_transaction_vm.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'package:domain/entity/rewards_my_detail_entity.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/load_state_layout.dart';
  8. import 'package:widgets/picker/option_pick_util.dart';
  9. import 'package:widgets/widget_export.dart';
  10. import './rewards_transaction_state.dart';
  11. import './rewards_transaction_repository.dart';
  12. part 'rewards_transaction_vm.g.dart';
  13. @riverpod
  14. class RewardsTransactionVm extends _$RewardsTransactionVm {
  15. late RewardsTransactionRepository rewardsTransactionRepository;
  16. bool _needShowPlaceholder = false; //是否展示LoadingView
  17. // Refresh 控制器
  18. final EasyRefreshController refreshController = EasyRefreshController(
  19. controlFinishRefresh: true, //允许刷新
  20. controlFinishLoad: true, //允许加载
  21. );
  22. RewardsTransactionState initState() {
  23. return RewardsTransactionState();
  24. }
  25. @override
  26. RewardsTransactionState build() {
  27. // 引入数据仓库
  28. rewardsTransactionRepository =
  29. ref.read(rewardsTransactionRepositoryProvider);
  30. // 初始化状态
  31. RewardsTransactionState state = initState();
  32. // 初始化列表数据
  33. return state;
  34. }
  35. // 初始化页面数据
  36. initPageData({int? id}) {
  37. Log.d("----property_news_vm-----initPageData");
  38. getListData(id: id);
  39. }
  40. // 上拉加载
  41. Future onLoadData() async {
  42. Log.d("----property_news_vm-----initListData");
  43. getListData();
  44. }
  45. // 去新闻详情页
  46. void goNewsDetail(String item) {
  47. Log.d(item);
  48. // PropertyPage.startInstance(context: context, item: item);
  49. }
  50. // 重试请求
  51. Future retryRequest({int? id}) async {
  52. _needShowPlaceholder = true;
  53. getListData(id: id);
  54. }
  55. //刷新页面状态
  56. void changeLoadingState(LoadState loadState, String? errorMsg) {
  57. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  58. }
  59. // 获取list 列表数据
  60. void getListData<T>({int? id}) async {
  61. Log.d("加载listData数据---------------start-----");
  62. try {
  63. //请求网络
  64. Map<String, dynamic> params = {"id": id};
  65. Log.d("请求参数------$params");
  66. final result =
  67. await rewardsTransactionRepository.fetchPropertyNewsList(params);
  68. Log.d("请求完成结果------${result.data}");
  69. //校验成功失败
  70. if (result.isSuccess) {
  71. state = state.copyWith(
  72. detailInfo: result.data as RewardsMyDetailEntity,
  73. );
  74. changeLoadingState(LoadState.State_Success, null);
  75. } else {
  76. String errorMessage = result.errorMsg!;
  77. changeLoadingState(LoadState.State_Error, errorMessage);
  78. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  79. }
  80. } catch (e) {
  81. ToastEngine.show("Error: $e");
  82. }
  83. }
  84. // // 下拉刷新
  85. // Future refreshListData() async {
  86. // Log.d("----property_news_vm-----refreshListData ");
  87. // // await Future.delayed(const Duration(seconds: 2));
  88. // state = state.copyWith(curPage: 1, pageSize: 10);
  89. // // ref.invalidateSelf();
  90. // // ref.invalidate(propertyNewsVmProvider);
  91. // getListData();
  92. // }
  93. }