garagesale_vm.dart 4.6 KB

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