newsfeed_tabs.dart 3.9 KB

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