newsfeed_vm.dart 4.7 KB

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