services_main_vm.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'dart:async';
  2. import 'package:cpt_services/modules/services/repair/repair_page.dart';
  3. import 'package:cpt_services/modules/services/services_page.dart';
  4. import 'package:domain/entity/service_category_entity.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/ext_dart.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/load_state_layout.dart';
  13. import 'package:widgets/my_text_view.dart';
  14. import 'package:widgets/widget_export.dart';
  15. import 'package:widgets/dialog/dialog_content_wrap.dart';
  16. import '../../../respository/services_respository.dart';
  17. import '../../../router/page/services_page_router.dart';
  18. import 'services_main_page.dart';
  19. import 'services_main_state.dart';
  20. part 'services_main_vm.g.dart';
  21. @riverpod
  22. class ServiceMainVm extends _$ServiceMainVm {
  23. late ServicesRespository serviceRespositoryInstance;
  24. int _detailId = 0;
  25. int _detailServiceTypeCode = 0;
  26. bool _needShowPlaceholder = true; //是否展示LoadingView
  27. int _page = 1; // 当前页
  28. int _limit = 10; // 每页数量
  29. int _count = 0; // 总条数
  30. int _totalnum = 130;
  31. int totalnum = 50;
  32. // Refresh 控制器
  33. final EasyRefreshController refreshController = EasyRefreshController(
  34. controlFinishRefresh: true, //允许刷新
  35. controlFinishLoad: true, //允许加载
  36. );
  37. ServiceMainState initState() {
  38. return ServiceMainState(
  39. datas: null,
  40. );
  41. }
  42. @override
  43. ServiceMainState build(){
  44. // 引入数据仓库
  45. serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
  46. final state = initState();
  47. Log.d("--------------------------build---------------------");
  48. return state;
  49. }
  50. //刷新页面状态
  51. void changeLoadingState(LoadState loadState, String? errorMsg) {
  52. state = state.copyWith(
  53. loadingState: loadState,
  54. errorMessage: errorMsg
  55. );
  56. }
  57. // 初始化页面数据
  58. initPageData() {
  59. Log.d("----for_sale_vm-----initPageData ${state.loadingState}");
  60. onRefresh();
  61. }
  62. // 上拉加载 更多
  63. Future loadMore() async {
  64. _page++;
  65. getServicesData();
  66. }
  67. // 下拉刷新
  68. Future onRefresh() async {
  69. _page = 1;
  70. getServicesData();
  71. }
  72. // 手动进行刷新
  73. Future triggerRefresh() async {
  74. Log.d("trggerRefresh");
  75. refreshController.callRefresh();
  76. }
  77. // 手动进行刷新
  78. Future directRefresh() async {
  79. state = state.copyWith(datas:null);
  80. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  81. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  82. refreshController.callRefresh();
  83. refreshController.resetFooter();
  84. _page = 1;
  85. _needShowPlaceholder = true;
  86. getServicesData();
  87. }
  88. // 重试请求
  89. Future retryRequest() async {
  90. _page = 1;
  91. _needShowPlaceholder = true;
  92. getServicesData();
  93. }
  94. Future getServicesData<T>() async {
  95. if (_needShowPlaceholder) {
  96. changeLoadingState(LoadState.State_Loading, null);
  97. }
  98. try {
  99. Map<String, dynamic> params = {
  100. "parent_id": null,
  101. };
  102. final result = await serviceRespositoryInstance.fetchServiceCateGoryList(params);
  103. if(result.isSuccess){
  104. state = state.copyWith(datas: result.list as List<ServiceCategoryEntity>);
  105. changeLoadingState(LoadState.State_Success, null);
  106. refreshController.finishRefresh();
  107. } else {
  108. String errorMessage = result.errorMsg!;
  109. changeLoadingState(LoadState.State_Error, errorMessage);
  110. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  111. }
  112. } catch (e) {
  113. ToastEngine.show("Error: $e");
  114. }
  115. // // 最后赋值
  116. _needShowPlaceholder = false;
  117. }
  118. // 去付费服务/维修服务 列表页
  119. goToPaidOrRepairListPage(BuildContext context, int id, String type, ServiceCategoryEntity entity){
  120. state = state.copyWith(
  121. curCategory: entity
  122. );
  123. if(id == 1){
  124. ServicesPage.startInstance(parentCategoryId: 1);
  125. }else if(id == 4){
  126. RepairPage.startInstance(parentCategoryId: 4);
  127. }else {
  128. ToastEngine.show("暂未开放");
  129. }
  130. }
  131. }