my_following_tabs.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 'my_following_vm.dart';
  15. class MyFollowingTabs extends HookConsumerWidget {
  16. List tabsList;
  17. Widget? Function(BuildContext)? tabItemBuilder;
  18. dynamic? tabsRouter;
  19. void Function(int)? onClickAction;
  20. EdgeInsets? margin;
  21. MyFollowingTabs({
  22. Key? key,
  23. required this.tabsList,
  24. this.tabsRouter,
  25. this.onClickAction,
  26. this.tabItemBuilder,
  27. this.margin = const EdgeInsets.only(top: 10, bottom: 10, left: 20, right: 20),
  28. }) : super(key: key);
  29. Widget _buildTabItem(BuildContext context, WidgetRef ref, vm,state, item, index, int activeTabCounter) {
  30. final activeTabIdx = tabsRouter!.activeIndex;
  31. // final activeTabCounter = ref.watch(myFollowingVmProvider.select((state) =>
  32. // state.tabsList![index] == 'follow'? state.followerCount: state.followerCount
  33. // ));
  34. return Container(
  35. width: MediaQuery.of(context).size.width / tabsList.length - margin!.left - margin!.right,
  36. height: 43,
  37. margin: margin,
  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. mainAxisAlignment: MainAxisAlignment.center,
  52. children: [
  53. Expanded(
  54. child: Container(
  55. alignment: Alignment.center,
  56. child: Row(
  57. mainAxisAlignment: MainAxisAlignment.center,
  58. children: [
  59. MyTextView(
  60. item,
  61. fontSize: 16,
  62. textAlign: TextAlign.center,
  63. isFontMedium: true,
  64. textColor: index == activeTabIdx ? Colors.white :ColorUtils.string2Color("#000000"),
  65. ),
  66. MyTextView(
  67. // '(${activeTabCounter.toString()})',
  68. '(${activeTabCounter.toString()})',
  69. fontSize: 16,
  70. textAlign: TextAlign.center,
  71. isFontMedium: true,
  72. textColor: index == activeTabIdx ? Colors.white :ColorUtils.string2Color("#000000"),
  73. ),
  74. ],
  75. ),
  76. ).onTap((){
  77. onClickAction?.call(index);
  78. }),
  79. ),
  80. ],
  81. ),
  82. );
  83. }
  84. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm, state){
  85. List tabsList = state.tabsList;
  86. int tabsLength = tabsList.length;
  87. return List.generate(tabsLength, (index) {
  88. final followCount = state.followCount;
  89. final followerCount = state.followerCount;
  90. final activeTabCounter = (index == 0? followCount: followerCount);
  91. return _buildTabItem(context, ref, vm, state, tabsList[index], index, activeTabCounter);
  92. });
  93. }
  94. @override
  95. Widget build(BuildContext context, WidgetRef ref) {
  96. final vm = ref.read(myFollowingVmProvider.notifier);
  97. final state = ref.watch(myFollowingVmProvider);
  98. // 使用useEffect钩子
  99. // useEffect(() {
  100. // print('副作用函数执行');
  101. // 这里是副作用逻辑
  102. // 返回清理函数
  103. // return () {
  104. // print('清理函数执行');
  105. // };
  106. // }, []); // 空依赖列表意味着这个副作用只在组件挂载时执行一次
  107. return SingleChildScrollView(
  108. scrollDirection: Axis.horizontal,
  109. physics: const BouncingScrollPhysics(),
  110. clipBehavior: Clip.none,
  111. child: Row(
  112. mainAxisSize: MainAxisSize.max,
  113. mainAxisAlignment: MainAxisAlignment.center,
  114. children: _buildTabs(context, ref, vm, state),
  115. ).constrained(
  116. maxWidth: MediaQuery.of(context).size.width
  117. ),
  118. );
  119. }
  120. }