garagesale_tabs.dart 3.6 KB

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