newsfeed_tabs.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:cpt_community/modules/newsfeed/newsfeed_vm.dart';
  2. import 'package:cs_resources/theme/app_colors_theme.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/color_utils.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:widgets/my_load_image.dart';
  11. import 'package:widgets/ext/ex_widget.dart';
  12. import 'package:widgets/my_text_view.dart';
  13. import 'package:widgets/my_appbar.dart';
  14. import '../community/community_vm.dart';
  15. import '../garagesale/garagesale_vm.dart';
  16. class NewsfeedTabs extends HookConsumerWidget {
  17. List tabsList;
  18. Widget? Function(BuildContext)? tabItemBuilder;
  19. void Function(int)? onClickAction;
  20. NewsfeedTabs({
  21. Key? key,
  22. required this.tabsList,
  23. this.onClickAction,
  24. this.tabItemBuilder
  25. }) : super(key: key);
  26. Widget _buildTabItem(BuildContext context, WidgetRef ref, vm, item, index) {
  27. // Log.d("NewsfeedTabs _buildTabItem index: ${vm.state.tabsList}");
  28. // Log.d("NewsfeedTabs _buildTabItem index: ${vm.state.activeTabIdx}");
  29. // 监听 activeTabIdx 的变化
  30. final activeTabIdx = ref.watch(communityVmProvider.select((state) => state.activeTabIdx));
  31. // Log.d("NewsfeedTabs _buildTabItem activeTabIdx: $activeTabIdx");
  32. return Container(
  33. width: MediaQuery.of(context).size.width / vm.state.tabsList.length - 30,
  34. height: 43,
  35. padding: const EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 10),
  36. decoration: index==activeTabIdx? BoxDecoration(
  37. color: index==activeTabIdx? context.appColors.btnBgDefault: ColorUtils.string2Color("#F2F3F6"),
  38. borderRadius: BorderRadius.circular(20),
  39. boxShadow: [
  40. BoxShadow(
  41. color: Colors.grey.withOpacity(0.5),
  42. spreadRadius: 1,
  43. blurRadius: 5,
  44. offset: const Offset(0, 2), // changes position of shadow
  45. ),
  46. ],
  47. ): null,
  48. child: Row(
  49. children: [
  50. Expanded(
  51. child: Container(
  52. alignment: Alignment.center,
  53. child: MyTextView(
  54. item,
  55. fontSize: 16,
  56. textAlign: TextAlign.center,
  57. isFontMedium: true,
  58. textColor: index == activeTabIdx ? Colors.white :ColorUtils.string2Color("#000000"),
  59. ),
  60. ),
  61. ),
  62. ],
  63. ),
  64. ).onTap((){
  65. print("22222 $index");
  66. onClickAction?.call(index);
  67. });
  68. }
  69. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm){
  70. int tabsLength = tabsList.length;
  71. if(tabsLength>0){
  72. return List.generate(tabsLength, (index) {
  73. return _buildTabItem(context, ref, vm, tabsList[index], index);
  74. });
  75. }else{
  76. return [
  77. const SizedBox.shrink()
  78. ];
  79. }
  80. }
  81. @override
  82. Widget build(BuildContext context, WidgetRef ref) {
  83. final vm = ref.read(communityVmProvider.notifier);
  84. // 使用useEffect钩子
  85. // useEffect(() {
  86. // print('副作用函数执行');
  87. // // 这里是副作用逻辑
  88. // // vm.initPropData(tabsList, tabItemBuilder, onClickAction);
  89. // // 返回清理函数
  90. // return () {
  91. // print('清理函数执行');
  92. // };
  93. // }, []); // 空依赖列表意味着这个副作用只在组件挂载时执行一次
  94. return SingleChildScrollView(
  95. scrollDirection: Axis.horizontal,
  96. physics: const BouncingScrollPhysics(),
  97. clipBehavior: Clip.none,
  98. child: Row(
  99. mainAxisSize: MainAxisSize.max,
  100. mainAxisAlignment: MainAxisAlignment.center,
  101. children: _buildTabs(context, ref, vm),
  102. ).constrained(
  103. maxWidth: MediaQuery.of(context).size.width
  104. ),
  105. );
  106. }
  107. }