garage_vm.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/toast/toast_engine.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:router/path/router_path.dart';
  7. import 'package:shared/utils/color_utils.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:auto_route/auto_route.dart';
  10. import 'garage_page.dart';
  11. import 'garage_state.dart';
  12. import 'garage_repository.dart';
  13. part 'garage_vm.g.dart';
  14. @riverpod
  15. class GarageVm extends _$GarageVm {
  16. late GarageRepository garageRepository;
  17. get topSectionsData => state.topSectionsData;
  18. GarageState initState() {
  19. return GarageState(
  20. activeIndex: 0,
  21. topSectionsData: [
  22. {
  23. "title": "News Feed",
  24. "icon": Assets.communityNesFeed,
  25. "pageStartInstanceFn": CommunityPage.startInstance,
  26. "page": const CommunityPage(),
  27. },
  28. {
  29. "title": "Garage Sale",
  30. "icon": Assets.communityGarageSale,
  31. "pageStartInstanceFn": GaragePage.startInstance,
  32. "page": const GaragePage(),
  33. },
  34. ],
  35. tabsList: [
  36. {
  37. 'title': 'For Sale',
  38. 'icon': null,
  39. 'active': true,
  40. 'activeTitleColor': Colors.white,
  41. 'activeTitleFontSize': 16,
  42. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  43. },
  44. {
  45. 'title': 'For Rent',
  46. 'icon': null,
  47. 'active': false,
  48. 'activeTitleColor': Colors.white,
  49. 'activeTitleFontSize': 16,
  50. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  51. },
  52. ],
  53. list: [
  54. {
  55. 'id':1,
  56. 'avator': Assets.communityCamera,
  57. 'title': '发动机上课士大夫',
  58. 'isFollow': false,
  59. 'content': '经典福克斯附件是的开飞机迪斯科封禁端口是否建立四道口附近 ……[More]',
  60. 'imageUrls': [],
  61. 'time': 'June 17,2016 at 7:23 p.m.',
  62. 'isLike': true,
  63. 'likeno': 12
  64. },
  65. {
  66. 'id':2,
  67. 'avator': Assets.communityCamera,
  68. 'title': '分等级付给的积分多少',
  69. 'isFollow': true,
  70. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  71. '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'],
  72. 'time': 'June 17,2016 at 7:23 p.m.',
  73. 'isLike': true,
  74. 'likeno': 12
  75. },
  76. ]
  77. );
  78. }
  79. @override
  80. GarageState build(){
  81. // 引入数据仓库
  82. garageRepository = ref.read(garageRepositoryProvider);
  83. // 初始化状态
  84. GarageState state = initState();
  85. return state;
  86. }
  87. // 上拉加载
  88. Future onLoadData() async {
  89. Log.d("----garage_vm-----initListData");
  90. // await Future.delayed(const Duration(seconds: 2));
  91. // if(state.list.length >= state.filterCount){
  92. // return;
  93. // }else {
  94. // int curPage = state.curPage + 1;
  95. // state = state.copyWith(curPage: curPage,);
  96. // getListData();
  97. // }
  98. // getListData();
  99. }
  100. // 获取list 列表数据
  101. void getListData<T>() async {
  102. Log.d("加载listData数据---------------start-----");
  103. try {
  104. //请求网络
  105. Map<String, dynamic> params = {
  106. "curPage": state.curPage,
  107. "pageSize": state.pageSize,
  108. };
  109. Log.d("请求参数------$params");
  110. final result = await garageRepository.fetchGarageList(params);
  111. Log.d("请求完成结果------${result.data}");
  112. //校验成功失败
  113. if (result.isSuccess) {
  114. // state = state.copyWith(serverTime: result.data);
  115. state = state;
  116. ToastEngine.show("获取数据成功");
  117. } else {
  118. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  119. }
  120. } catch (e) {
  121. ToastEngine.show("Error: $e");
  122. }
  123. }
  124. // 下拉刷新
  125. Future refreshListData() async {
  126. Log.d("----garage_vm-----refreshListData ");
  127. // await Future.delayed(const Duration(seconds: 2));
  128. state = state.copyWith(curPage: 1, pageSize: 10);
  129. // ref.invalidateSelf();
  130. // ref.invalidate(garageVmProvider);
  131. getListData();
  132. }
  133. // 切换tab
  134. handlerChangeTab(int index, tabsRouter) {
  135. tabsRouter.setActiveIndex(index);
  136. // state = state.copyWith(tabsRouter: tabsRouter ,activeTabIdx: index);
  137. Log.d("garage_vm handlerChangeTab--index: $index");
  138. }
  139. // 点击发布的按钮 跳转到发布的页面
  140. void handlerGotoPost(context){
  141. // ComponentServiceManager().communityService.startCommunityPage();
  142. AutoRouter.of(context).pushNamed(RouterPath.garageSalePost);
  143. }
  144. handlerChangeCommunityType(context, int index){
  145. if(index == 0){
  146. // 返回到 newsfeed 页面
  147. Navigator.pop(context);
  148. }
  149. }
  150. }