services_vm.dart 8.8 KB

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