services_vm.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // 获取当前pageView 的vm
  100. getCurrentPageViewVm(int? pageViewIdx){
  101. if(pageViewIdx !=null){
  102. return providerMap[pageViewIdx];
  103. }else {
  104. return getCurrentPageViewVm(state.currentPageViewIdx);
  105. }
  106. }
  107. // 返回当前 pageView 页面的 vm
  108. // 判断当前pageview 页面正处于显示状态
  109. Future isCurrentPageViewShowing(int pageViewIdx) async{
  110. // 延迟获取结果
  111. bool isShowing = await Future.delayed(const Duration(milliseconds: 500), (){
  112. return state.currentPageViewIdx == pageViewIdx;
  113. });
  114. return isShowing;
  115. }
  116. Future<void> handlerShowChooseGarageCategoryDialog(BuildContext context, List<Map<String, dynamic>> garageCategoryList) async{
  117. await DialogEngine.show(
  118. tag: "chooseGarageSaleCategory",
  119. position: DialogPosition.center,
  120. widget: AppCustomDialog(
  121. message: '',
  122. dialogWidth: MediaQuery.of(context).size.width * 0.8,
  123. // contentBoxMaxHeight: 350,
  124. // contentBoxMinHeight: 300,
  125. isShowConfirmBtn: garageCategoryList!.length > 0 ? true: false,
  126. confirmTxt: "Ok",
  127. messageBuilder: (BuildContext context){
  128. return Container(
  129. color: context.appColors.textWhite,
  130. child: Column(
  131. mainAxisAlignment: MainAxisAlignment.start,
  132. crossAxisAlignment: CrossAxisAlignment.start,
  133. children: garageCategoryList!.length > 0 ? [
  134. MyCheckboxGroup(
  135. isSingleSelect: _isSingleSelect,
  136. labelStyle: const TextStyle(
  137. fontSize: 16,
  138. fontWeight: FontWeight.w500,
  139. ),
  140. items: garageCategoryList!,
  141. defaultSelectedItems: [],
  142. onChanged: (List<Map<String, dynamic>> selectedItems){
  143. Log.d("----MyCheckboxGroup onChanged $selectedItems");
  144. _currentSelectedGarageCategory = selectedItems;
  145. }
  146. )
  147. ]: [
  148. Container(
  149. child: CircularProgressIndicator(
  150. strokeWidth: 3,
  151. valueColor: AlwaysStoppedAnimation(context.appColors.textDarkGray),
  152. ),
  153. )
  154. ],
  155. ),
  156. );
  157. },
  158. isShowCancelBtn:false,
  159. confirmAction: (){
  160. // 点击了确定
  161. Log.d("----点击了确定按钮");
  162. int? categoryId;
  163. if(_isSingleSelect){
  164. if(_currentSelectedGarageCategory.length > 0){
  165. categoryId = _currentSelectedGarageCategory[0]['id'];
  166. }
  167. }
  168. providerMap[state.currentPageViewIdx]
  169. ..setCurrentQueryParams({
  170. "category_id": _queryParams?[state.currentPageViewIdx]?['categoryId'],
  171. })
  172. ..directRefresh();
  173. },
  174. )
  175. );
  176. }
  177. // 搜索
  178. handlerSearch(String value){
  179. Log.d("services_vm 中 搜索 value: $value");
  180. _queryParams?[state.currentPageViewIdx]?['keyword'] = value;
  181. providerMap[state.currentPageViewIdx]
  182. ..setCurrentQueryParams({
  183. "keyword": _queryParams?[state.currentPageViewIdx]?['keyword'],
  184. })
  185. ..directRefresh();
  186. }
  187. // 点击了导航栏的 like btn
  188. handlerClickNavbarLikeBtn(BuildContext? context){
  189. _queryParams[state.currentPageViewIdx]?['is_liked'] = !(_queryParams?[state.currentPageViewIdx]?['is_liked']??false);
  190. // 控制外层滚动和内层滚动
  191. handlerNestedScrollViewScroll();
  192. // providerMap[state.currentPageViewIdx]
  193. // ..setCurrentQueryParams({
  194. // "is_liked": _queryParams?[state.currentPageViewIdx]?['is_liked'],
  195. // })
  196. // ..directRefresh();
  197. }
  198. handlerNestedScrollViewScroll({double? outerOffset, double? innerOffset=0.0, bool? isOuterScrollAnimated=false, bool? isInnerScrollAnimated=false}){
  199. if(outerOffset !=null){
  200. if(isOuterScrollAnimated!){
  201. extendedNestedScrollViewKey.currentState?.outerController.animateTo(
  202. outerOffset,
  203. duration: const Duration(seconds: 1),
  204. curve: Curves.easeIn,
  205. );
  206. }else {
  207. extendedNestedScrollViewKey.currentState?.outerController.jumpTo(
  208. outerOffset,
  209. );
  210. }
  211. }
  212. if(innerOffset !=null){
  213. extendedNestedScrollViewKey.currentState?.innerPositions.forEach((position) {
  214. if(isInnerScrollAnimated!){
  215. position.animateTo(innerOffset,
  216. duration: Duration(seconds: 1), curve: Curves.easeIn);
  217. }else {
  218. position.jumpTo(innerOffset);
  219. }
  220. });
  221. }
  222. }
  223. }