rewards_detail_vm.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:domain/entity/rewards_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_detail_state.dart';
  11. import './rewards_detail_repository.dart';
  12. part 'rewards_detail_vm.g.dart';
  13. @riverpod
  14. class RewardsDetailVm extends _$RewardsDetailVm {
  15. late RewardsDetailRepository rewardsDetailRepository;
  16. bool _needShowPlaceholder = false; //是否展示LoadingView
  17. // Refresh 控制器
  18. final EasyRefreshController refreshController = EasyRefreshController(
  19. controlFinishRefresh: true, //允许刷新
  20. controlFinishLoad: true, //允许加载
  21. );
  22. RewardsDetailState initState() {
  23. return RewardsDetailState();
  24. }
  25. @override
  26. RewardsDetailState build() {
  27. // 引入数据仓库
  28. rewardsDetailRepository = ref.read(rewardsDetailRepositoryProvider);
  29. // 初始化状态
  30. RewardsDetailState state = initState();
  31. // 初始化列表数据
  32. Log.d("state.list------${state.detailInfo}");
  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 rewardsDetailRepository.fetchPropertyNewsList(params);
  68. Log.d("请求完成结果------${result.data}");
  69. //校验成功失败
  70. if (result.isSuccess) {
  71. state = state.copyWith(
  72. detailInfo: result.data as RewardsDetailEntity,
  73. );
  74. Log.d("123------${state.detailInfo}");
  75. changeLoadingState(LoadState.State_Success, null);
  76. } else {
  77. String errorMessage = result.errorMsg!;
  78. changeLoadingState(LoadState.State_Error, errorMessage);
  79. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  80. }
  81. } catch (e) {
  82. ToastEngine.show("Error: $e");
  83. }
  84. }
  85. // // 下拉刷新
  86. // Future refreshListData() async {
  87. // Log.d("----property_news_vm-----refreshListData ");
  88. // // await Future.delayed(const Duration(seconds: 2));
  89. // state = state.copyWith(curPage: 1, pageSize: 10);
  90. // // ref.invalidateSelf();
  91. // // ref.invalidate(propertyNewsVmProvider);
  92. // getListData();
  93. // }
  94. }