custom_tabs.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'package:cpt_community/components/custom_tabs_state.dart';
  2. import 'package:cpt_community/components/custom_tabs_state.dart';
  3. import 'package:cpt_community/modules/newsfeed/newsfeed_vm.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:auto_route/auto_route.dart';
  7. import 'package:flutter_hooks/flutter_hooks.dart';
  8. import 'package:hooks_riverpod/hooks_riverpod.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/color_utils.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/my_load_image.dart';
  13. import 'package:widgets/ext/ex_widget.dart';
  14. import 'package:widgets/my_text_view.dart';
  15. import 'package:widgets/my_appbar.dart';
  16. import '../modules/community/community_vm.dart';
  17. import '../modules/garagesale/garagesale_vm.dart';
  18. import 'custom_tabs_state.dart';
  19. import 'custom_tabs_vm.dart';
  20. class CustomTabs extends HookConsumerWidget {
  21. List tabsList;
  22. Widget? Function(BuildContext)? tabItemBuilder;
  23. VoidCallback? onClickAction;
  24. CustomTabs({
  25. Key? key,
  26. required this.tabsList,
  27. this.onClickAction,
  28. this.tabItemBuilder
  29. }) : super(key: key);
  30. Widget _buildTabItem(BuildContext context, WidgetRef ref, vm, item, index) {
  31. // 监听 activeIndex 的变化
  32. final activeTabIndex = ref.watch(customTabsVmProvider.select((state) => state.activeTabIndex));
  33. // final activeTabItemIndex = item['active'];
  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==activeTabIndex? BoxDecoration(
  39. color: index==activeTabIndex? 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['title'],
  57. fontSize: 16,
  58. textAlign: TextAlign.center,
  59. isFontMedium: true,
  60. textColor: index == activeTabIndex ? Colors.white :ColorUtils.string2Color("#000000"),
  61. ),
  62. ),
  63. ),
  64. ],
  65. ),
  66. ).onTap((){
  67. vm.handlerClickTab(index, item);
  68. });
  69. }
  70. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm, tablist){
  71. int currentUseTag = ref.watch(customTabsVmProvider.select((state) => state.useTag!));
  72. ref.listen(customTabsVmProvider.select((state) => state.useTag), (previous, next) {
  73. // 设置当前的 tabsList
  74. if(next == 1){
  75. // Garage Sale
  76. List tabsList = ref.read(newsfeedVmProvider).tabsList;
  77. }else if(next == 0){
  78. List tabsList = ref.read(garagesaleVmProvider).tabsList;
  79. }
  80. });
  81. int tabsLength = tabsList.length;
  82. print("000328942424----${tabsList}");
  83. return List.generate(tabsLength, (index) {
  84. return _buildTabItem(context, ref, vm, tabsList[index], index);
  85. });
  86. }
  87. @override
  88. Widget build(BuildContext context, WidgetRef ref) {
  89. final vm = ref.watch(customTabsVmProvider.notifier);
  90. // 使用useEffect钩子
  91. useEffect(() {
  92. print('副作用函数执行');
  93. // 这里是副作用逻辑
  94. vm.initPropData(tabsList, tabItemBuilder, onClickAction);
  95. // 返回清理函数
  96. return () {
  97. print('清理函数执行');
  98. };
  99. }, []); // 空依赖列表意味着这个副作用只在组件挂载时执行一次
  100. return SingleChildScrollView(
  101. scrollDirection: Axis.horizontal,
  102. physics: const BouncingScrollPhysics(),
  103. clipBehavior: Clip.none,
  104. child: Row(
  105. mainAxisSize: MainAxisSize.max,
  106. mainAxisAlignment: MainAxisAlignment.center,
  107. children: _buildTabs(context, ref, vm, tabsList),
  108. ).constrained(
  109. maxWidth: MediaQuery.of(context).size.width
  110. ),
  111. );
  112. }
  113. }