newsfeed_vm.dart 5.1 KB

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