community_page.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'package:flutter/material.dart';
  2. import 'package:auto_route/auto_route.dart';
  3. import 'package:flutter/rendering.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 'package:cs_resources/theme/app_colors_theme.dart';
  14. import 'package:widgets/widget_export.dart';
  15. import '../../router/page/community_page_router.dart';
  16. import 'community_vm.dart';
  17. @RoutePage()
  18. class CommunityPage extends HookConsumerWidget {
  19. const CommunityPage({Key? key}) : super(key: key);
  20. //启动当前页面
  21. static void startInstance({BuildContext? context}) {
  22. if (context != null) {
  23. context.router.push(const CommunityPageRoute());
  24. } else {
  25. appRouter.push(const CommunityPageRoute());
  26. }
  27. }
  28. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, tabsRouter) {
  29. final topSectionsData = vm.topSectionsData;
  30. final currentTabIdx = tabsRouter.activeIndex;
  31. // 监听 curIdx 的变化
  32. // final curIdx = ref.watch(communityVmProvider.select((value) => value.curIdx));
  33. return Container(
  34. color: Colors.white,
  35. padding: const EdgeInsets.only(top: 30, bottom: 30),
  36. child: Center(
  37. child: Row(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. crossAxisAlignment: CrossAxisAlignment.center,
  40. children: List.generate(topSectionsData.length, (index) {
  41. final item = topSectionsData[index];
  42. return Flexible(
  43. flex: 1,
  44. child: Column(
  45. children: [
  46. Container(
  47. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  48. height: 70,
  49. decoration: BoxDecoration(
  50. // color: currentTabIdx == index ? ColorUtils.string2Color('#E6F2FF') : Colors.white,
  51. shape: BoxShape.circle, // 设置为圆形
  52. boxShadow: currentTabIdx == index ? [
  53. BoxShadow(
  54. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  55. blurRadius: 5, // 设置模糊半径
  56. spreadRadius: 0.05, // 控制阴影扩散
  57. offset: const Offset(0, 4), // 设置阴影偏移量
  58. ),
  59. ] : [],// 未选中时无阴影,
  60. ),
  61. child: MyAssetImage(
  62. item['icon'],
  63. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  64. height: 70,
  65. ).onTap(() {
  66. tabsRouter.setActiveIndex(index);
  67. },
  68. type: ClickType.throttle,
  69. ),
  70. ),
  71. SizedBox.fromSize(size: const Size(0, 9)),
  72. Text(
  73. item['title'],
  74. maxLines: 1, // 设置最大行数为2
  75. overflow: TextOverflow.ellipsis, // 超出部分用省略号表示
  76. style: TextStyle(
  77. fontSize: 15.0,
  78. color: currentTabIdx == index ? ColorUtils.string2Color('#4161D0'):Colors.black,
  79. fontWeight: FontWeight.w500
  80. ), // 设置字体大小
  81. ),
  82. ],
  83. ),
  84. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  85. }),
  86. ),
  87. ),
  88. );
  89. }
  90. @override
  91. Widget build(BuildContext context, WidgetRef ref) {
  92. final vm = ref.read(communityVmProvider.notifier);
  93. return Scaffold(
  94. appBar: MyAppBar.appBar(
  95. context,
  96. "Community",
  97. backgroundColor: context.appColors.whiteBG,
  98. ),
  99. backgroundColor: context.appColors.backgroundDefault,
  100. body: NestedScrollView(
  101. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled){
  102. return <Widget>[
  103. SliverToBoxAdapter(
  104. child: Container(
  105. child: const SizedBox.shrink(),
  106. )
  107. ),
  108. SliverToBoxAdapter(
  109. child: Consumer(
  110. builder: (context, ref, _) {
  111. final tabsRouter = ref.watch(communityVmProvider).tabsRouter;
  112. final vm = ref.read(communityVmProvider.notifier);
  113. Log.d("community_page---buildTopSection ---${tabsRouter}");
  114. Log.d("community_page---buildTopSection ---${vm.state.curIdx}");
  115. // return _buildTopSection(context, ref, vm, tabsRouter);
  116. if(tabsRouter != null){
  117. return _buildTopSection(context, ref, vm, tabsRouter);
  118. }else {
  119. return const SizedBox.shrink();
  120. }
  121. },
  122. ),
  123. ),
  124. ];
  125. },
  126. body: AutoTabsRouter.pageView(
  127. routes: const [
  128. NewsfeedPageRoute(),
  129. GaragesalePageRoute(),
  130. ],
  131. builder: (context, child, pageController) {
  132. final tabsRouter = AutoTabsRouter.of(context);
  133. // 设置当前的 useTag 0 newsFeed 1 garageSale
  134. vm.setCurrentUseTag(tabsRouter.activeIndex);
  135. // 将tabsRouter 放入 CommunityVmProvider 中
  136. vm.setTabsRouterAndPageController(tabsRouter, pageController);
  137. return Column(
  138. children: [
  139. // _buildTopSection(context, ref, vm, tabsRouter),
  140. Expanded(
  141. child: child,
  142. ),
  143. ],
  144. );
  145. },
  146. ),
  147. ),
  148. );
  149. }
  150. }