garage_vm.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'package:cpt_community/modules/community/community_page.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/path/router_path.dart';
  8. import 'package:shared/utils/color_utils.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:auto_route/auto_route.dart';
  11. import 'package:widgets/dialog/app_default_dialog.dart';
  12. import 'package:widgets/dialog/app_custom_dialog.dart';
  13. import 'package:widgets/app_check_box.dart';
  14. import 'package:widgets/my_checkbox_group.dart';
  15. import 'garage_page.dart';
  16. import 'garage_state.dart';
  17. import 'garage_repository.dart';
  18. part 'garage_vm.g.dart';
  19. @riverpod
  20. class GarageVm extends _$GarageVm {
  21. late GarageRepository garageRepository;
  22. get topSectionsData => state.topSectionsData;
  23. GarageState initState() {
  24. return GarageState(
  25. activeIndex: 0,
  26. topSectionsData: [
  27. {
  28. "title": "News Feed",
  29. "icon": Assets.communityNesFeed,
  30. "pageStartInstanceFn": CommunityPage.startInstance,
  31. "page": const CommunityPage(),
  32. },
  33. {
  34. "title": "Garage Sale",
  35. "icon": Assets.communityGarageSale,
  36. "pageStartInstanceFn": GaragePage.startInstance,
  37. "page": const GaragePage(),
  38. },
  39. ],
  40. tabsList: [
  41. {
  42. 'title': 'For Sale',
  43. 'icon': null,
  44. 'active': true,
  45. 'activeTitleColor': Colors.white,
  46. 'activeTitleFontSize': 16,
  47. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  48. },
  49. {
  50. 'title': 'For Rent',
  51. 'icon': null,
  52. 'active': false,
  53. 'activeTitleColor': Colors.white,
  54. 'activeTitleFontSize': 16,
  55. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  56. },
  57. ],
  58. categoryList: [
  59. {
  60. 'id': '1',
  61. 'label': 'Kids',
  62. },
  63. {
  64. 'id': '2',
  65. 'label': 'Homeware',
  66. },
  67. {
  68. 'id': '3',
  69. 'label': 'Fashion',
  70. },
  71. {
  72. 'id': '4',
  73. 'label': 'Electronics',
  74. },
  75. {
  76. 'id': '5',
  77. 'label': 'Sports',
  78. },
  79. {
  80. 'id': '6',
  81. 'label': 'Furniture',
  82. },
  83. {
  84. 'id': '7',
  85. 'label': 'Others',
  86. },
  87. ],
  88. list: [
  89. {
  90. 'id':1,
  91. 'avator': Assets.communityCamera,
  92. 'title': '发动机上课士大夫',
  93. 'isFollow': false,
  94. 'content': '经典福克斯附件是的开飞机迪斯科封禁端口是否建立四道口附近 ……[More]',
  95. 'imageUrls': [],
  96. 'time': 'June 17,2016 at 7:23 p.m.',
  97. 'isLike': true,
  98. 'likeno': 12
  99. },
  100. {
  101. 'id':2,
  102. 'avator': Assets.communityCamera,
  103. 'title': '分等级付给的积分多少',
  104. 'isFollow': true,
  105. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  106. 'imageUrls': ['https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg','https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg','https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg'],
  107. 'time': 'June 17,2016 at 7:23 p.m.',
  108. 'isLike': true,
  109. 'likeno': 12
  110. },
  111. ]
  112. );
  113. }
  114. @override
  115. GarageState build(){
  116. // 引入数据仓库
  117. garageRepository = ref.read(garageRepositoryProvider);
  118. // 初始化状态
  119. GarageState state = initState();
  120. return state;
  121. }
  122. // 上拉加载
  123. Future onLoadData() async {
  124. Log.d("----garage_vm-----initListData");
  125. // await Future.delayed(const Duration(seconds: 2));
  126. // if(state.list.length >= state.filterCount){
  127. // return;
  128. // }else {
  129. // int curPage = state.curPage + 1;
  130. // state = state.copyWith(curPage: curPage,);
  131. // getListData();
  132. // }
  133. // getListData();
  134. }
  135. // 获取list 列表数据
  136. void getListData<T>() async {
  137. Log.d("加载listData数据---------------start-----");
  138. try {
  139. //请求网络
  140. Map<String, dynamic> params = {
  141. "curPage": state.curPage,
  142. "pageSize": state.pageSize,
  143. };
  144. Log.d("请求参数------$params");
  145. final result = await garageRepository.fetchGarageList(params);
  146. Log.d("请求完成结果------${result.data}");
  147. //校验成功失败
  148. if (result.isSuccess) {
  149. // state = state.copyWith(serverTime: result.data);
  150. state = state;
  151. ToastEngine.show("获取数据成功");
  152. } else {
  153. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  154. }
  155. } catch (e) {
  156. ToastEngine.show("Error: $e");
  157. }
  158. }
  159. // 下拉刷新
  160. Future refreshListData() async {
  161. Log.d("----garage_vm-----refreshListData ");
  162. // await Future.delayed(const Duration(seconds: 2));
  163. state = state.copyWith(curPage: 1, pageSize: 10);
  164. // ref.invalidateSelf();
  165. // ref.invalidate(garageVmProvider);
  166. getListData();
  167. }
  168. // 选择 category
  169. handlerChooseCategory(BuildContext context) async {
  170. await DialogEngine.show(
  171. tag: "choosegaragesalecategory",
  172. position: DialogPosition.center,
  173. widget: AppCustomDialog(
  174. message: '',
  175. confirmTxt: "Ok",
  176. messageBuilder: (BuildContext context){
  177. return Container(
  178. // width: 500,
  179. height: 400,
  180. color: Colors.white,
  181. child: Column(
  182. mainAxisAlignment: MainAxisAlignment.start,
  183. crossAxisAlignment: CrossAxisAlignment.start,
  184. children: [
  185. MyCheckboxGroup(
  186. isSingleSelect: false,
  187. labelStyle: const TextStyle(
  188. fontSize: 16,
  189. fontWeight: FontWeight.w500,
  190. ),
  191. items: state.categoryList,
  192. onChanged: (List<Map<String, dynamic>> selectedItems){
  193. Log.d("----MyCheckboxGroup onChanged $selectedItems");
  194. }
  195. )
  196. ],
  197. ),
  198. );
  199. },
  200. isShowCancelBtn:false,
  201. confirmAction: (){
  202. },
  203. )
  204. );
  205. }
  206. // 切换tab
  207. handlerChangeTab(int index, tabsRouter) {
  208. tabsRouter.setActiveIndex(index);
  209. // state = state.copyWith(tabsRouter: tabsRouter ,activeTabIdx: index);
  210. Log.d("garage_vm handlerChangeTab--index: $index");
  211. }
  212. // 点击发布的按钮 跳转到发布的页面
  213. void handlerGotoPost(context){
  214. // ComponentServiceManager().communityService.startCommunityPage();
  215. AutoRouter.of(context).pushNamed(RouterPath.garageSalePost);
  216. }
  217. handlerChangeCommunityType(context, int index){
  218. if(index == 0){
  219. // 返回到 newsfeed 页面
  220. Navigator.pop(context);
  221. }
  222. }
  223. }