my_posts_tabs.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'package:cpt_community/modules/my_posts/my_posts_newsfeed/my_posts_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 'package:auto_route/auto_route.dart';
  15. class MyPostsTab extends HookConsumerWidget {
  16. List tabsList;
  17. Widget? Function(BuildContext)? tabItemBuilder;
  18. dynamic? tabsRouter;
  19. void Function(int)? onClickAction;
  20. MyPostsTab({
  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. Log.d("MyPostsTab _buildTabItem index: ${vm.state.tabsList}");
  29. final activeTabIdx = AutoTabsRouter.of(context).activeIndex;
  30. final isLast = index == tabsList.length - 1;
  31. return Row(
  32. children: [
  33. Container(
  34. width: MediaQuery.of(context).size.width / vm.state.tabsList.length - 5,
  35. padding: const EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 10),
  36. decoration: index==activeTabIdx? BoxDecoration(
  37. color: context.appColors.textWhite,
  38. // borderRadius: BorderRadius.circular(20),
  39. border: Border(
  40. bottom: BorderSide(
  41. color: context.appColors.textPrimary,
  42. width: 2,
  43. ),
  44. ),
  45. ): null,
  46. child: Row(
  47. children: [
  48. Expanded(
  49. child: Column(
  50. children: [
  51. Container(
  52. alignment: Alignment.center,
  53. child: MyTextView(
  54. item!['publish_num'].toString(),
  55. fontSize: 15,
  56. textAlign: TextAlign.center,
  57. isFontMedium: true,
  58. textColor: index == activeTabIdx ? context.appColors.textPrimary : context.appColors.textBlack,
  59. ),
  60. ),
  61. const SizedBox(height: 5,),
  62. Container(
  63. alignment: Alignment.center,
  64. child: MyTextView(
  65. item!['title'],
  66. fontSize: 15,
  67. textAlign: TextAlign.center,
  68. isFontMedium: true,
  69. textColor: index == activeTabIdx ? context.appColors.textPrimary : context.appColors.textBlack,
  70. ),
  71. ),
  72. ],
  73. ).onTap((){
  74. Log.d("newsfeed_tabs 中点击的tab index: $index");
  75. onClickAction?.call(index);
  76. }),
  77. ),
  78. ],
  79. ),
  80. ),
  81. isLast? const SizedBox.shrink(): Container(
  82. width: 1,
  83. height: 60,
  84. color: context.appColors.grayBgE9,
  85. )
  86. ],
  87. );
  88. }
  89. List<Widget> _buildTabs(BuildContext context, WidgetRef ref, vm){
  90. int tabsLength = tabsList.length;
  91. if(tabsLength>0){
  92. return List.generate(tabsLength, (index) {
  93. return _buildTabItem(context, ref, vm, tabsList[index], index);
  94. });
  95. }else{
  96. return [
  97. const SizedBox.shrink()
  98. ];
  99. }
  100. }
  101. @override
  102. Widget build(BuildContext context, WidgetRef ref) {
  103. final vm = ref.read(myPostsNewsfeedVmProvider.notifier);
  104. // 使用useEffect钩子
  105. // useEffect(() {
  106. // print('副作用函数执行');
  107. // // 这里是副作用逻辑
  108. // // vm.initPropData(tabsList, tabItemBuilder, onClickAction);
  109. // // 返回清理函数
  110. // return () {
  111. // print('清理函数执行');
  112. // };
  113. // }, []); // 空依赖列表意味着这个副作用只在组件挂载时执行一次
  114. return SingleChildScrollView(
  115. scrollDirection: Axis.horizontal,
  116. physics: const BouncingScrollPhysics(),
  117. clipBehavior: Clip.none,
  118. child: Row(
  119. mainAxisSize: MainAxisSize.max,
  120. mainAxisAlignment: MainAxisAlignment.center,
  121. children: _buildTabs(context, ref, vm),
  122. ).constrained(
  123. maxWidth: MediaQuery.of(context).size.width
  124. ),
  125. );
  126. }
  127. }