community_vm.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'package:cpt_community/components/comments_dialog.dart';
  2. import 'package:cpt_community/router/page/community_page_router.dart';
  3. import 'package:cs_resources/generated/assets.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. import 'package:router/path/router_path.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:auto_route/auto_route.dart';
  12. import 'package:widgets/dialog/app_custom_dialog.dart';
  13. import 'package:widgets/my_checkbox_group.dart';
  14. import '../garage/garagesale_post/garagesale_post_page.dart';
  15. import '../my_following/my_following_page.dart';
  16. import '../my_posts/my_posts_page.dart';
  17. import 'community_page.dart';
  18. import 'community_state.dart';
  19. import 'newsfeed_post/newsfeed_post_page.dart';
  20. part 'community_vm.g.dart';
  21. @riverpod
  22. class CommunityVm extends _$CommunityVm {
  23. get topSectionsData => state.topSectionsData;
  24. CommunityVmState initState() {
  25. return CommunityVmState(
  26. currentCategoryIdx: 0,
  27. currentPageViewIdx: 0,
  28. lastGarageTabIdx: 0,
  29. lastNewsfeedTabIdx: 0,
  30. newsFeedTabsList: [
  31. "News",
  32. "Following",
  33. "For You",
  34. ],
  35. garageSaleTabsList: [
  36. "For Sale",
  37. "For Rent",
  38. ],
  39. );
  40. }
  41. @override
  42. CommunityVmState build(){
  43. final state = initState();
  44. // 第一帧渲染完成
  45. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  46. });
  47. Log.d("--------------------------build---------------------");
  48. return state;
  49. }
  50. tabsRouterChange(){
  51. Log.d("----tabsRouterChange---${tabsRouterKey.currentState?.controller?.activeIndex}-");
  52. state = state.copyWith(currentPageViewIdx: tabsRouterKey.currentState?.controller?.activeIndex ?? 0);
  53. }
  54. // 切换tab
  55. handlerChangeTab(int tabIndex, TabsRouter? tabsRouter, int? categoryIdx) {
  56. tabsRouter = (tabsRouter?? tabsRouterKey.currentState?.controller)!;
  57. categoryIdx = categoryIdx ?? state.currentCategoryIdx;
  58. if(categoryIdx == 0){
  59. tabsRouter.setActiveIndex(tabIndex);
  60. }else {
  61. tabsRouter.setActiveIndex(state.newsFeedTabsList!.length + tabIndex);
  62. }
  63. }
  64. // 切换news feed和garage sale
  65. handlerSwitchNewsfeedOrGaragesale( int categoryIdx, BuildContext? context, TabsRouter? tabsRouter){
  66. tabsRouter = (tabsRouter?? tabsRouterKey.currentState?.controller)!;
  67. categoryIdx = categoryIdx;
  68. if(categoryIdx == 0){
  69. tabsRouter.setActiveIndex(state.lastNewsfeedTabIdx);
  70. }else if (categoryIdx == 1){
  71. tabsRouter.setActiveIndex(state.newsFeedTabsList!.length + state.lastGarageTabIdx);
  72. }
  73. }
  74. // 设置当前的cat类型
  75. setCurrentCategoryIdx(BuildContext? context, int categoryIdx, int? lastNewsfeedTabIdx, int? lastGarageTabIdx){
  76. state = state.copyWith(
  77. currentCategoryIdx: categoryIdx,
  78. lastNewsfeedTabIdx: lastNewsfeedTabIdx?? state.lastNewsfeedTabIdx,
  79. lastGarageTabIdx: lastGarageTabIdx?? state.lastGarageTabIdx
  80. );
  81. }
  82. // 判断当前pageview 页面正处于显示状态
  83. Future isCurrentPageViewShowing(int pageViewIdx) async{
  84. // 延迟获取结果
  85. bool isShowing = await Future.delayed(const Duration(milliseconds: 500), (){
  86. return state.currentPageViewIdx == pageViewIdx;
  87. });
  88. return isShowing;
  89. }
  90. // 选择 garage sale 导航栏点击 选择分类
  91. handlerChooseGarageCategory(BuildContext context) async {
  92. List<Map<String, dynamic>> categoryList = [
  93. {
  94. 'id': '1',
  95. 'label': 'Kids',
  96. },
  97. {
  98. 'id': '2',
  99. 'label': 'Homeware',
  100. },
  101. {
  102. 'id': '3',
  103. 'label': 'Fashion',
  104. },
  105. {
  106. 'id': '4',
  107. 'label': 'Electronics',
  108. },
  109. {
  110. 'id': '5',
  111. 'label': 'Sports',
  112. },
  113. {
  114. 'id': '6',
  115. 'label': 'Furniture',
  116. },
  117. {
  118. 'id': '7',
  119. 'label': 'Others',
  120. },
  121. ];
  122. await DialogEngine.show(
  123. tag: "chooseGarageSaleCategory",
  124. position: DialogPosition.center,
  125. widget: AppCustomDialog(
  126. message: '',
  127. dialogWidth: MediaQuery.of(context).size.width * 0.8,
  128. // contentBoxMaxHeight: 350,
  129. // contentBoxMinHeight: 300,
  130. confirmTxt: "Ok",
  131. messageBuilder: (BuildContext context){
  132. return Container(
  133. color: context.appColors.textWhite,
  134. child: Column(
  135. mainAxisAlignment: MainAxisAlignment.start,
  136. crossAxisAlignment: CrossAxisAlignment.start,
  137. children: [
  138. MyCheckboxGroup(
  139. isSingleSelect: false,
  140. labelStyle: const TextStyle(
  141. fontSize: 16,
  142. fontWeight: FontWeight.w500,
  143. ),
  144. items: categoryList,
  145. onChanged: (List<Map<String, dynamic>> selectedItems){
  146. Log.d("----MyCheckboxGroup onChanged $selectedItems");
  147. }
  148. )
  149. ],
  150. ),
  151. );
  152. },
  153. isShowCancelBtn:false,
  154. confirmAction: (){
  155. },
  156. )
  157. );
  158. }
  159. // 点击了导航栏的 like btn
  160. handlerClickNavbarLikeBtn(BuildContext? context){
  161. if(state.currentCategoryIdx ==0){
  162. //
  163. ToastEngine.show("点击了 newsfeed like");
  164. }else if(state.currentCategoryIdx == 1){
  165. //
  166. ToastEngine.show("点击了 garagesale like");
  167. }
  168. }
  169. // 点击了导航栏的 filter btn
  170. handlerClickNavbarFilterBtn(BuildContext? context,){
  171. ToastEngine.show("点击 filter");
  172. handlerChooseGarageCategory(context!);
  173. }
  174. // 点击发布的按钮 跳转到 newsfeed 发布的页面
  175. void handlerGotoNewsfeedPost(context){
  176. // AutoRouter.of(context).pushNamed(RouterPath.newsFeedPost);
  177. NewsfeedPostPage.startInstance();
  178. // MyPostsPage.startInstance();
  179. // MyFollowingPage.startInstance();
  180. }
  181. // 点击发布的按钮 跳转到garagesale 发布的页面
  182. void handlerGotoGaragePost(context){
  183. GaragesalePostPage.startInstance();
  184. }
  185. }