newsfeed_tabs.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'package:cs_resources/theme/app_colors_theme.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:auto_route/auto_route.dart';
  4. import 'package:flutter_hooks/flutter_hooks.dart';
  5. import 'package:hooks_riverpod/hooks_riverpod.dart';
  6. import 'package:router/ext/auto_router_extensions.dart';
  7. import 'package:shared/utils/color_utils.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/my_load_image.dart';
  10. import 'package:widgets/ext/ex_widget.dart';
  11. import 'package:widgets/my_text_view.dart';
  12. import 'package:widgets/my_appbar.dart';
  13. import 'community_page.dart';
  14. import 'community_vm.dart';
  15. class NewsfeedTabs extends HookConsumerWidget {
  16. List<String> tabsList;
  17. Widget? Function(BuildContext)? tabItemBuilder;
  18. dynamic? tabsRouter;
  19. void Function(Map<String, dynamic>)? onClickAction;
  20. EdgeInsets? margin;
  21. NewsfeedTabs({
  22. Key? key,
  23. required this.tabsList,
  24. this.tabsRouter,
  25. this.onClickAction,
  26. this.tabItemBuilder,
  27. this.margin = const EdgeInsets.only(top: 10, bottom: 10, left: 20, right: 20),
  28. }) : super(key: key);
  29. Widget _buildTabItem(BuildContext context, WidgetRef ref, vm, item, index) {
  30. final providerCommunityState = ref.watch(communityVmProvider);
  31. int lastNewsFeedTabIdx = 0;
  32. int lastGarageTabIdx = 0;
  33. final newsfeedTabCount = ref.read(communityVmProvider.notifier).state!.newsFeedTabsList!.length;
  34. // 监听 activeTabIdx 的变化
  35. final activePageIdx = ref.watch(communityVmProvider.select((state) => state.currentPageViewIdx));
  36. int currentCatgoryIdx = activePageIdx >= newsfeedTabCount? 1: 0;
  37. int curTabIdx = 0;
  38. // final activeTabIdx = tabsRouter!.activeIndex;
  39. // final activeTabIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  40. // 监听 currentPageViewIdx 的变化
  41. ref.listen(communityVmProvider.select((state) => state.currentPageViewIdx), (oldValue, newValue){
  42. // Log.d("--111--oldValue: $oldValue, newValue: $newValue---------------");
  43. // Log.d("--111--lastNewsFeedTabIdx: $lastNewsFeedTabIdx, lastGarageTabIdx: $lastGarageTabIdx---------------");
  44. if(oldValue! < newsfeedTabCount){
  45. if( newValue >= newsfeedTabCount ){
  46. // 由 newsfeed 切换到 garage sale 记录newsFeed 最后的 tabidx
  47. lastNewsFeedTabIdx = oldValue;
  48. lastGarageTabIdx = newValue - newsfeedTabCount;
  49. currentCatgoryIdx = 1;
  50. Log.d("-从newsfeed 切换到 garagesale-----------currentCatgoryIdx $currentCatgoryIdx-------lastNewsFeedTabIdx: $lastNewsFeedTabIdx, lastGarageTabIdx: $lastGarageTabIdx---------------");
  51. }else {
  52. lastNewsFeedTabIdx = newValue;
  53. lastGarageTabIdx = providerCommunityState.lastGarageTabIdx;
  54. }
  55. }
  56. if(oldValue! >= newsfeedTabCount){
  57. if(newValue < newsfeedTabCount){
  58. // 由 garage sale 切换到 newsfeed 记录garage sale 最后的 tabidx
  59. lastGarageTabIdx = oldValue - newsfeedTabCount;
  60. lastNewsFeedTabIdx = newValue;
  61. currentCatgoryIdx = 0;
  62. Log.d("-从garagesale 切换到 newsfeed---------------currentCatgoryIdx $currentCatgoryIdx------lastNewsFeedTabIdx: $lastNewsFeedTabIdx, lastGarageTabIdx: $lastGarageTabIdx---------------");
  63. }else {
  64. lastGarageTabIdx = newValue - newsfeedTabCount;
  65. lastNewsFeedTabIdx = providerCommunityState.lastNewsfeedTabIdx;
  66. }
  67. }
  68. // Log.d("-666---currentCatgoryIdx----$currentCatgoryIdx-----lastNewsFeedTabIdx: $lastNewsFeedTabIdx, lastGarageTabIdx: $lastGarageTabIdx---------------");
  69. ref.read(communityVmProvider.notifier).setCurrentCategoryIdx(
  70. context,
  71. currentCatgoryIdx,
  72. lastNewsFeedTabIdx,
  73. lastGarageTabIdx,
  74. );
  75. });
  76. if(activePageIdx >= newsfeedTabCount){
  77. // garage sale
  78. curTabIdx = activePageIdx - newsfeedTabCount;
  79. }else {
  80. // news feed
  81. curTabIdx = activePageIdx;
  82. }
  83. return Container(
  84. width: MediaQuery.of(context).size.width / tabsList.length - margin!.left - margin!.right,
  85. height: 43,
  86. margin: margin,
  87. decoration: index==curTabIdx? BoxDecoration(
  88. color: index==curTabIdx? context.appColors.btnBgDefault: ColorUtils.string2Color("#F2F3F6"),
  89. borderRadius: BorderRadius.circular(20),
  90. boxShadow: [
  91. BoxShadow(
  92. color: Colors.grey.withOpacity(0.5),
  93. spreadRadius: 1,
  94. blurRadius: 5,
  95. offset: const Offset(0, 2), // changes position of shadow
  96. ),
  97. ],
  98. ): null,
  99. child: Row(
  100. children: [
  101. Expanded(
  102. child: Container(
  103. alignment: Alignment.center,
  104. child: MyTextView(
  105. item,
  106. fontSize: 16,
  107. textAlign: TextAlign.center,
  108. isFontMedium: true,
  109. textColor: index == curTabIdx ? Colors.white :ColorUtils.string2Color("#000000"),
  110. ),
  111. ).onTap((){
  112. // Log.d("----currentCatgoryIdx----$currentCatgoryIdx-------");
  113. onClickAction?.call({'currentCatgoryIdx':currentCatgoryIdx, 'tabIdx': index});
  114. }),
  115. ),
  116. ],
  117. ),
  118. );
  119. }
  120. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm){
  121. int tabsLength = tabsList.length;
  122. if(tabsLength>0){
  123. return List.generate(tabsLength, (index) {
  124. return _buildTabItem(context, ref, vm, tabsList[index], index);
  125. });
  126. }else{
  127. return [
  128. const SizedBox.shrink()
  129. ];
  130. }
  131. }
  132. @override
  133. Widget build(BuildContext context, WidgetRef ref) {
  134. final vm = ref.read(communityVmProvider.notifier);
  135. // 使用useEffect钩子
  136. // useEffect(() {
  137. // print('副作用函数执行');
  138. // // 这里是副作用逻辑
  139. // // vm.initPropData(tabsList, tabItemBuilder, onClickAction);
  140. // // 返回清理函数
  141. // return () {
  142. // print('清理函数执行');
  143. // };
  144. // }, []); // 空依赖列表意味着这个副作用只在组件挂载时执行一次
  145. return SingleChildScrollView(
  146. scrollDirection: Axis.horizontal,
  147. physics: const BouncingScrollPhysics(),
  148. clipBehavior: Clip.none,
  149. child: Row(
  150. mainAxisSize: MainAxisSize.max,
  151. mainAxisAlignment: MainAxisAlignment.center,
  152. crossAxisAlignment: CrossAxisAlignment.center,
  153. children: _buildTabs(context, ref, vm),
  154. ),
  155. );
  156. }
  157. }