repair_vm.dart 8.7 KB

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