services_vm.dart 9.4 KB

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