custom_tabs.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:cpt_community/components/custom_tabs_state.dart';
  2. import 'package:cpt_community/components/custom_tabs_state.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:auto_route/auto_route.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 'custom_tabs_state.dart';
  15. import 'custom_tabs_vm.dart';
  16. class CustomTabs extends HookConsumerWidget {
  17. List tabsList;
  18. int activeIndex = 0;
  19. Widget? Function(BuildContext)? tabItemBuilder;
  20. VoidCallback? onClickAction;
  21. CustomTabs({Key? key, required this.tabsList, required this.activeIndex, this.onClickAction, this.tabItemBuilder}) :
  22. super(key: key);
  23. Widget _buildTabItem(BuildContext context, WidgetRef ref, vm, item, index) {
  24. // 监听 activeIndex 的变化
  25. final activeIndex = ref.watch(customTabsVmProvider.select((state) => state.activeIndex));
  26. return Container(
  27. width: MediaQuery.of(context).size.width / vm.state.tabsList.length - 30,
  28. height: 43,
  29. padding: const EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 10),
  30. decoration: index==activeIndex? BoxDecoration(
  31. color: index==activeIndex? context.appColors.btnBgDefault: ColorUtils.string2Color("#F2F3F6"),
  32. borderRadius: BorderRadius.circular(20),
  33. boxShadow: [
  34. BoxShadow(
  35. color: Colors.grey.withOpacity(0.5),
  36. spreadRadius: 1,
  37. blurRadius: 5,
  38. offset: const Offset(0, 2), // changes position of shadow
  39. ),
  40. ],
  41. ): null,
  42. child: Row(
  43. children: [
  44. Expanded(
  45. child: Container(
  46. alignment: Alignment.center,
  47. child: MyTextView(
  48. item['title'],
  49. fontSize: 16,
  50. textAlign: TextAlign.center,
  51. isFontMedium: true,
  52. textColor: index == activeIndex ? Colors.white :ColorUtils.string2Color("#000000"),
  53. ),
  54. ),
  55. ),
  56. ],
  57. ),
  58. ).onTap((){
  59. vm.handlerClickTab(index, item);
  60. });
  61. }
  62. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm){
  63. tabsList = vm.state.tabsList;
  64. int tabsLength = tabsList.length;
  65. return List.generate(tabsLength, (index) {
  66. return _buildTabItem(context, ref, vm, tabsList[index], index);
  67. });
  68. }
  69. @override
  70. Widget build(BuildContext context, WidgetRef ref) {
  71. final vm = ref.read(customTabsVmProvider.notifier);
  72. // 初始化状态,只在第一次构建时执行
  73. if (vm.state.tabsList!.isEmpty) {
  74. vm.initPropData(tabsList, activeIndex, tabItemBuilder, onClickAction);
  75. }
  76. return SingleChildScrollView(
  77. scrollDirection: Axis.horizontal,
  78. physics: const BouncingScrollPhysics(),
  79. clipBehavior: Clip.none,
  80. child: Row(
  81. mainAxisSize: MainAxisSize.max,
  82. mainAxisAlignment: MainAxisAlignment.center,
  83. children: _buildTabs(context, ref, vm),
  84. ).constrained(
  85. maxWidth: MediaQuery.of(context).size.width
  86. ),
  87. );
  88. }
  89. }