repair_vm.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import 'package:cpt_services/modules/services/repair_homeService/home_service_vm.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_riverpod/flutter_riverpod.dart';
  7. import 'package:plugin_basic/constants/app_constant.dart';
  8. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  9. import 'package:plugin_platform/engine/sp/sp_util.dart';
  10. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  11. import 'package:riverpod_annotation/riverpod_annotation.dart';
  12. import 'package:shared/utils/log_utils.dart';
  13. import 'package:auto_route/auto_route.dart';
  14. import 'package:widgets/dialog/app_custom_dialog.dart';
  15. import 'package:widgets/my_checkbox_group.dart';
  16. import '../../../respository/services_respository.dart';
  17. import '../repair_history/history_vm.dart';
  18. import '../repair_inProgress/in_progress_vm.dart';
  19. import 'repair_page.dart';
  20. import 'repair_pageview_idx_data.dart';
  21. import 'repair_state.dart';
  22. part 'repair_vm.g.dart';
  23. @riverpod
  24. class RepairVm extends _$RepairVm {
  25. get topSectionsData => state.topSectionsData;
  26. late ServicesRespository repairRespositoryInstance;
  27. late Map<int, dynamic> providerMap = {};
  28. bool _isSingleSelect = true;
  29. List<Map<String, dynamic>> _currentSelectedGarageCategory = [];
  30. Map<int, Map<String, dynamic>> _queryParams = {};
  31. // 获取当前的查询参数
  32. getCurrentQueryParams(String key) {
  33. return _queryParams[state.currentPageViewIdx]?[key];
  34. }
  35. // 根据索引获取 Provider
  36. ProviderBase getProvider(int index) {
  37. return providerMap[index]!;
  38. }
  39. // 初始化 _queryParams
  40. queryParamsInit(){
  41. List<MapEntry<int, String>> repairEntriesList = RepairPageviewIdxData.values.entries.toList();
  42. repairEntriesList.asMap().forEach((index, value) {
  43. _queryParams[index] = {
  44. 'keyword': '',
  45. 'is_liked': '',
  46. 'page_view_idx': index,
  47. 'page_view_name': value,
  48. };
  49. });
  50. }
  51. RepairVmState initState() {
  52. return RepairVmState(
  53. currentPageViewIdx: 0,
  54. );
  55. }
  56. RepairVmState build(){
  57. // 引入数据仓库
  58. repairRespositoryInstance = ref.read(servicesRespositoryProvider);
  59. final state = initState();
  60. queryParamsInit();
  61. setCurrentPageViewIconStatus();
  62. getPageViewVm();
  63. Log.d("-------------repair vm-------------build---------------------");
  64. ref.onDispose((){
  65. providerMap = {};
  66. Log.d("-------------repair vm-------------dispose---------------------");
  67. });
  68. return state;
  69. }
  70. // 搜集pageView 对应的vm
  71. void getPageViewVm(){
  72. // 每次切换后需要重新获取 一组 pageView的 vm
  73. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  74. // 存入 一组 pageView的 vm
  75. RepairPageviewIdxData.values.forEach((key, value) {
  76. switch(key){
  77. case 0:
  78. providerMap[key] = ref.read(repairHomeServiceVmProvider.notifier);
  79. case 1:
  80. providerMap[key] = ref.read(repairInProgressVmProvider.notifier);
  81. case 2:
  82. providerMap[key] = ref.read(repairHistoryVmProvider.notifier);
  83. }
  84. });
  85. });
  86. }
  87. // 设置当前导航栏的 图标 等状态
  88. void setCurrentPageViewIconStatus(){
  89. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  90. Log.d("获取当前的导航栏 相关状态resMap444 ${providerMap[state.currentPageViewIdx]} ");
  91. Map<String, dynamic> resMap = providerMap[state.currentPageViewIdx]?.getCurrentQueryParams(null)??{};
  92. Log.d("获取当前的导航栏 相关状态resMap $resMap ");
  93. _queryParams[state.currentPageViewIdx] = resMap;
  94. });
  95. }
  96. tabsRouterChange(){
  97. // 设置当前导航栏的 图标 等状态
  98. Log.d("----tabsRouterChange---${tabsRouterKey.currentState?.controller?.activeIndex}-");
  99. state = state.copyWith(currentPageViewIdx: tabsRouterKey.currentState?.controller?.activeIndex ?? 0);
  100. setCurrentPageViewIconStatus();
  101. getPageViewVm();
  102. }
  103. handlerSwitchPageView(BuildContext? context, int pageViewIdx){
  104. tabsRouterKey.currentState?.controller?.setActiveIndex(pageViewIdx);
  105. }
  106. // 获取当前pageView 的vm
  107. getCurrentPageViewVm(int? pageViewIdx){
  108. if(pageViewIdx !=null){
  109. return providerMap[pageViewIdx];
  110. }else {
  111. return getCurrentPageViewVm(state.currentPageViewIdx);
  112. }
  113. }
  114. // 返回当前 pageView 页面的 vm
  115. // 判断当前pageview 页面正处于显示状态
  116. Future isCurrentPageViewShowing(int pageViewIdx) async{
  117. // 延迟获取结果
  118. bool isShowing = await Future.delayed(const Duration(milliseconds: 500), (){
  119. return state.currentPageViewIdx == pageViewIdx;
  120. });
  121. return isShowing;
  122. }
  123. Future<void> handlerShowChooseGarageCategoryDialog(BuildContext context, List<Map<String, dynamic>> garageCategoryList) async{
  124. await DialogEngine.show(
  125. tag: "chooseGarageSaleCategory",
  126. position: DialogPosition.center,
  127. widget: AppCustomDialog(
  128. message: '',
  129. dialogWidth: MediaQuery.of(context).size.width * 0.8,
  130. // contentBoxMaxHeight: 350,
  131. // contentBoxMinHeight: 300,
  132. isShowConfirmBtn: garageCategoryList!.length > 0 ? true: false,
  133. confirmTxt: "Ok",
  134. messageBuilder: (BuildContext context){
  135. return Container(
  136. color: context.appColors.textWhite,
  137. child: Column(
  138. mainAxisAlignment: MainAxisAlignment.start,
  139. crossAxisAlignment: CrossAxisAlignment.start,
  140. children: garageCategoryList!.length > 0 ? [
  141. MyCheckboxGroup(
  142. isSingleSelect: _isSingleSelect,
  143. labelStyle: const TextStyle(
  144. fontSize: 16,
  145. fontWeight: FontWeight.w500,
  146. ),
  147. items: garageCategoryList!,
  148. defaultSelectedItems: [],
  149. onChanged: (List<Map<String, dynamic>> selectedItems){
  150. Log.d("----MyCheckboxGroup onChanged $selectedItems");
  151. _currentSelectedGarageCategory = selectedItems;
  152. }
  153. )
  154. ]: [
  155. Container(
  156. child: CircularProgressIndicator(
  157. strokeWidth: 3,
  158. valueColor: AlwaysStoppedAnimation(context.appColors.textDarkGray),
  159. ),
  160. )
  161. ],
  162. ),
  163. );
  164. },
  165. isShowCancelBtn:false,
  166. confirmAction: (){
  167. // 点击了确定
  168. Log.d("----点击了确定按钮");
  169. int? categoryId;
  170. if(_isSingleSelect){
  171. if(_currentSelectedGarageCategory.length > 0){
  172. categoryId = _currentSelectedGarageCategory[0]['id'];
  173. }
  174. }
  175. providerMap[state.currentPageViewIdx]
  176. ..setCurrentQueryParams({
  177. "category_id": _queryParams?[state.currentPageViewIdx]?['categoryId'],
  178. })
  179. ..directRefresh();
  180. },
  181. )
  182. );
  183. }
  184. // 搜索
  185. handlerSearch(String value){
  186. Log.d("repair_vm 中 搜索 value: $value");
  187. _queryParams?[state.currentPageViewIdx]?['keyword'] = value;
  188. providerMap[state.currentPageViewIdx]
  189. ..setCurrentQueryParams({
  190. "keyword": _queryParams?[state.currentPageViewIdx]?['keyword'],
  191. })
  192. ..directRefresh();
  193. }
  194. // 点击了导航栏的 like btn
  195. handlerClickNavbarLikeBtn(BuildContext? context){
  196. _queryParams[state.currentPageViewIdx]?['is_liked'] = !(_queryParams?[state.currentPageViewIdx]?['is_liked']??false);
  197. // 控制外层滚动和内层滚动
  198. handlerNestedScrollViewScroll();
  199. // providerMap[state.currentPageViewIdx]
  200. // ..setCurrentQueryParams({
  201. // "is_liked": _queryParams?[state.currentPageViewIdx]?['is_liked'],
  202. // })
  203. // ..directRefresh();
  204. }
  205. handlerNestedScrollViewScroll({double? outerOffset, double? innerOffset=0.0, bool? isOuterScrollAnimated=false, bool? isInnerScrollAnimated=false}){
  206. if(outerOffset !=null){
  207. if(isOuterScrollAnimated!){
  208. extendedNestedScrollViewKey.currentState?.outerController.animateTo(
  209. outerOffset,
  210. duration: const Duration(seconds: 1),
  211. curve: Curves.easeIn,
  212. );
  213. }else {
  214. extendedNestedScrollViewKey.currentState?.outerController.jumpTo(
  215. outerOffset,
  216. );
  217. }
  218. }
  219. if(innerOffset !=null){
  220. extendedNestedScrollViewKey.currentState?.innerPositions.forEach((position) {
  221. if(isInnerScrollAnimated!){
  222. position.animateTo(innerOffset,
  223. duration: Duration(seconds: 1), curve: Curves.easeIn);
  224. }else {
  225. position.jumpTo(innerOffset);
  226. }
  227. });
  228. }
  229. }
  230. }