my_posts_vm.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:router/ext/auto_router_extensions.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'package:widgets/load_state_layout.dart';
  8. import 'package:widgets/widget_export.dart';
  9. import 'package:auto_route/auto_route.dart';
  10. import '../../../router/page/community_page_router.dart';
  11. import 'my_posts_page.dart';
  12. import 'my_posts_state.dart';
  13. part 'my_posts_vm.g.dart';
  14. @riverpod
  15. class MyPostsVm extends _$MyPostsVm {
  16. // late MyPostsRepository MyPostsRepositoryInstance;
  17. int _currentPageIndex = 0;
  18. bool _needShowPlaceholder = false; //是否展示LoadingView
  19. // Refresh 控制器
  20. final EasyRefreshController refreshController = EasyRefreshController(
  21. controlFinishRefresh: true, //允许刷新
  22. controlFinishLoad: true, //允许加载
  23. );
  24. MyPostsState initState() {
  25. return MyPostsState(
  26. currentPageViewIdx: 0,
  27. tabsList: [
  28. {
  29. 'code': 'newsFeed',
  30. 'title': 'News Feed',
  31. 'publish_num': 0,
  32. },
  33. {
  34. 'code': 'forSale',
  35. 'title': 'For Sale',
  36. 'publish_num': 0,
  37. },
  38. {
  39. 'code': 'forRent',
  40. 'title': 'For Rent',
  41. 'publish_num': 0,
  42. },
  43. ]
  44. );
  45. }
  46. @override
  47. MyPostsState build(){
  48. // 引入数据仓库
  49. // MyPostsRepositoryInstance = ref.read(newsRepositoryProvider);
  50. MyPostsState state = initState();
  51. Log.d("--------------------------build---------------------");
  52. return state;
  53. }
  54. tabsRouterChange(){
  55. Log.d("----tabsRouterChange---${myPostsPageTabsRouterKey.currentState?.controller?.activeIndex}-");
  56. state = state.copyWith(currentPageViewIdx: myPostsPageTabsRouterKey.currentState?.controller?.activeIndex ?? 0);
  57. }
  58. // 切换tab
  59. handlerChangeTab(int tabIndex, TabsRouter? tabsRouter,) {
  60. tabsRouter = (tabsRouter?? myPostsPageTabsRouterKey.currentState?.controller)!;
  61. tabsRouter.setActiveIndex(tabIndex);
  62. }
  63. setCurrentTabIndex(int activeTabIdx){
  64. state = state.copyWith(
  65. activeTabIdx: activeTabIdx,
  66. );
  67. }
  68. updateMyPostsTabsNum(String code, int num, int activeTabIdx){
  69. state = state.copyWith(
  70. tabsList: state.tabsList?.map((e) {
  71. if(e['code'] == code){
  72. e['publish_num'] = num;
  73. }
  74. return e;
  75. }).toList(),
  76. activeTabIdx: activeTabIdx,
  77. );
  78. }
  79. }