custom_tabs_vm.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'package:cpt_community/components/custom_tabs_state.dart';
  2. import 'package:cpt_community/modules/community/community_vm.dart';
  3. import 'package:cpt_community/modules/garagesale/garagesale_vm.dart';
  4. import 'package:cpt_community/modules/newsfeed/newsfeed_vm.dart';
  5. import 'package:cs_resources/generated/assets.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:flutter_riverpod/flutter_riverpod.dart';
  8. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  9. import 'package:riverpod_annotation/riverpod_annotation.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. part 'custom_tabs_vm.g.dart';
  12. @riverpod
  13. class CustomTabsVm extends _$CustomTabsVm {
  14. CustomTabsState initState(int? useTag, List? tabsList, tabItemBuilder, onClickAction) {
  15. CustomTabsState state = CustomTabsState(
  16. useTag: useTag??0,
  17. activeTabIndex: 0,
  18. tabsList: tabsList ?? [],
  19. tabItemBuilder:tabItemBuilder,
  20. onClickAction: onClickAction
  21. );
  22. return state;
  23. }
  24. @override
  25. CustomTabsState build(){
  26. print("5656566---${ref.read(communityVmProvider).useTag}");
  27. // 当前的 useTag
  28. // setUseTag(ref.read(communityVmProvider).useTag as int);
  29. // 监听 UserVM 的状态变化
  30. ref.listen(communityVmProvider.select((community) => community.useTag), (previous, next) {
  31. print('community useTag changed from $previous to $next');
  32. // 设置当前的 tabsList
  33. if(next == 1){
  34. // Garage Sale
  35. setUseTag(1);
  36. }else if(next == 0){
  37. setUseTag(0);
  38. }
  39. });
  40. CustomTabsState state = initState(0,[], null, null);
  41. return state;
  42. }
  43. // 设置当前的 useTag
  44. void setUseTag(int useTag) {
  45. CustomTabsState newState = state.copyWith(useTag: useTag);
  46. state = newState;
  47. }
  48. // 设置当前的tabsList
  49. void setTabsList() {
  50. List currentTabsList = [];
  51. if(state.useTag == 0){
  52. // 找到 newsfeed 中的 tabsList
  53. currentTabsList = ref.read(newsfeedVmProvider).tabsList;
  54. }else if(state.useTag == 1){
  55. currentTabsList = ref.read(garagesaleVmProvider).tabsList;
  56. }
  57. CustomTabsState newState = state.copyWith(tabsList: currentTabsList);
  58. state = newState;
  59. }
  60. // 将组件构造器中的参数保存到state中
  61. Future<void> initPropData(tabsList,tabItemBuilder, onClickAction) async {
  62. // 获取 community 中的 useTag
  63. // state = state.copyWith(useTag: ref.watch(communityVmProvider).useTag);
  64. CustomTabsState newState = state.copyWith(
  65. tabsList: tabsList,
  66. tabItemBuilder: tabItemBuilder,
  67. onClickAction: onClickAction
  68. );
  69. state = newState;
  70. }
  71. // 点击tab
  72. void handlerClickTab(int index, item){
  73. setTabsList();
  74. // List newState = state.tabsList!.asMap().entries.map((entry) {
  75. // Map<String, dynamic> obj = entry.value;
  76. // int idx = entry.key;
  77. //
  78. // if (obj['title'] == item['title'] && index == idx) {
  79. // return {
  80. // ...obj,
  81. // 'active': true,
  82. // };
  83. // } else {
  84. // return {
  85. // ...obj,
  86. // 'active': false,
  87. // };
  88. // }
  89. // }).toList();
  90. //
  91. // // if(state.useTag)
  92. // // 修改当前item 的active
  93. // state = state.copyWith(tabsList: newState);
  94. state = state.copyWith(activeTabIndex: index);
  95. print("切换后新的 sate, ${state.tabsList}");
  96. // ref.invalidate(customTabsVmProvider);
  97. }
  98. }