newsfeed_vm.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'package:cpt_community/modules/newsfeed/newsfeed_post/newsfeed_post_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/componentRouter/component_service_manager.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 'newsfeed_state.dart';
  12. import 'newsfeed_repository.dart';
  13. part 'newsfeed_vm.g.dart';
  14. @riverpod
  15. class NewsfeedVm extends _$NewsfeedVm {
  16. late NewsfeedRepository newsfeedRepository;
  17. NewsfeedState initState() {
  18. return NewsfeedState(
  19. useTag: 0,
  20. activeIndex: 0,
  21. tabsList: [
  22. {
  23. 'title': 'News',
  24. 'icon': null,
  25. 'active': true,
  26. 'activeTitleColor': Colors.white,
  27. 'activeTitleFontSize': 16,
  28. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  29. },
  30. {
  31. 'title': 'Following',
  32. 'icon': null,
  33. 'active': false,
  34. 'activeTitleColor': Colors.white,
  35. 'activeTitleFontSize': 16,
  36. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  37. },
  38. {
  39. 'title': 'For You',
  40. 'icon': null,
  41. 'activeTitleColor': Colors.white,
  42. 'activeTitleFontSize': 16,
  43. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  44. }
  45. ],
  46. list: [
  47. {
  48. 'id':1,
  49. 'avator': Assets.communityCamera,
  50. 'title': 'William Jefferson',
  51. 'isFollow': false,
  52. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  53. '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'],
  54. 'time': 'June 17,2016 at 7:23 p.m.',
  55. 'isLike': true,
  56. 'likeno': 12
  57. },
  58. {
  59. 'id':2,
  60. 'avator': Assets.communityCamera,
  61. 'title': 'William fdsaf的飞洒发生的',
  62. 'isFollow': true,
  63. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  64. 'imageUrls': [],
  65. 'time': 'June 17,2016 at 7:23 p.m.',
  66. 'isLike': true,
  67. 'likeno': 12
  68. },
  69. {
  70. 'id':3,
  71. 'avator': Assets.communityCamera,
  72. 'title': 'Fsjfkds dfsk',
  73. 'isFollow': false,
  74. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  75. 'imageUrls': [],
  76. 'time': 'June 17,2016 at 7:23 p.m.',
  77. 'isLike': false,
  78. 'likeno': 12
  79. },
  80. {
  81. 'id':4,
  82. 'avator': Assets.communityCamera,
  83. 'title': 'Fsjfkds dfsk',
  84. 'isFollow': false,
  85. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  86. 'imageUrls': [],
  87. 'time': 'June 17,2016 at 7:23 p.m.',
  88. 'isLike': false,
  89. 'likeno': 12
  90. },
  91. {
  92. 'id':5,
  93. 'avator': Assets.communityCamera,
  94. 'title': 'Fsjfkds dfsk',
  95. 'isFollow': false,
  96. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  97. 'imageUrls': [],
  98. 'time': 'June 17,2016 at 7:23 p.m.',
  99. 'isLike': false,
  100. 'likeno': 12
  101. },
  102. {
  103. 'id':6,
  104. 'avator': Assets.communityCamera,
  105. 'title': 'Fsjfkds dfsk',
  106. 'isFollow': false,
  107. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  108. 'imageUrls': [],
  109. 'time': 'June 17,2016 at 7:23 p.m.',
  110. 'isLike': false,
  111. 'likeno': 12
  112. },
  113. {
  114. 'id':7,
  115. 'avator': Assets.communityCamera,
  116. 'title': '放大发大水',
  117. 'isFollow': false,
  118. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  119. 'imageUrls': [],
  120. 'time': 'June 17,2016 at 7:23 p.m.',
  121. 'isLike': false,
  122. 'likeno': 12
  123. },
  124. ]
  125. );
  126. }
  127. @override
  128. NewsfeedState build(){
  129. // 引入数据仓库
  130. newsfeedRepository = ref.read(newsfeedRepositoryProvider);
  131. // 初始化状态
  132. NewsfeedState state = initState();
  133. return state;
  134. }
  135. // 上拉加载
  136. Future onLoadData() async {
  137. Log.d("----newsfeed_vm-----initListData");
  138. // await Future.delayed(const Duration(seconds: 2));
  139. // if(state.list.length >= state.filterCount){
  140. // return;
  141. // }else {
  142. // int curPage = state.curPage + 1;
  143. // state = state.copyWith(curPage: curPage,);
  144. // getListData();
  145. // }
  146. // getListData();
  147. }
  148. // 获取list 列表数据
  149. void getListData<T>() async {
  150. Log.d("加载listData数据---------------start-----");
  151. try {
  152. //请求网络
  153. Map<String, dynamic> params = {
  154. "curPage": state.curPage,
  155. "pageSize": state.pageSize,
  156. };
  157. Log.d("请求参数------$params");
  158. final result = await newsfeedRepository.fetchNewsfeedList(params);
  159. Log.d("请求完成结果------${result.data}");
  160. //校验成功失败
  161. if (result.isSuccess) {
  162. // state = state.copyWith(serverTime: result.data);
  163. state = state;
  164. ToastEngine.show("获取数据成功");
  165. } else {
  166. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  167. }
  168. } catch (e) {
  169. ToastEngine.show("Error: $e");
  170. }
  171. }
  172. // 下拉刷新
  173. Future refreshListData() async {
  174. Log.d("----property_news_vm-----refreshListData ");
  175. // await Future.delayed(const Duration(seconds: 2));
  176. state = state.copyWith(curPage: 1, pageSize: 10);
  177. // ref.invalidateSelf();
  178. // ref.invalidate(newsfeedVmProvider);
  179. getListData();
  180. }
  181. // 点击发布的按钮 跳转到发布的页面
  182. void handlerGotoPost(context){
  183. // ComponentServiceManager().communityService.startCommunityPage();
  184. // AutoRouter.of(context).pushNamed(RouterPath.newsFeedPost);
  185. NewsfeedPostPage.startInstance();
  186. }
  187. // 点击tab
  188. void handlerClickTab(int index, item){
  189. state = state.copyWith(activeIndex: index);
  190. print("切换后新的 sate, ${state.tabsList}");
  191. // ref.invalidate(customTabsVmProvider);
  192. }
  193. }