garage_tabs.dart 3.5 KB

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