community_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:auto_route/auto_route.dart';
  4. import 'package:flutter/rendering.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:cs_resources/theme/app_colors_theme.dart';
  15. import 'package:widgets/widget_export.dart';
  16. import '../../router/page/community_page_router.dart';
  17. import 'newsfeed_tabs.dart';
  18. import 'community_vm.dart';
  19. import 'customSilverHeaderTabs.dart';
  20. @RoutePage()
  21. class CommunityPage extends HookConsumerWidget {
  22. const CommunityPage({Key? key}) : super(key: key);
  23. //启动当前页面
  24. static void startInstance({BuildContext? context}) {
  25. if (context != null) {
  26. context.router.push(const CommunityPageRoute());
  27. } else {
  28. appRouter.push(const CommunityPageRoute());
  29. }
  30. }
  31. bool _isPreventScroll(ScrollNotification notification, WidgetRef ref){
  32. // 检查当前页面是否是可见的
  33. bool isDownOrUp = notification.metrics.axis == Axis.vertical;
  34. if (notification is UserScrollNotification) {
  35. // 检查滚动方向
  36. switch (notification.direction) {
  37. case ScrollDirection.forward:
  38. print('Scrolling down');
  39. break;
  40. case ScrollDirection.reverse:
  41. print('Scrolling up');
  42. break;
  43. case ScrollDirection.idle:
  44. print('Scrolling stopped');
  45. break;
  46. }
  47. } else if (notification is ScrollUpdateNotification) {
  48. // 检查滚动位置变化
  49. double currentScrollPosition = notification.metrics.pixels;
  50. double maxScrollExtent = notification.metrics.maxScrollExtent;
  51. // 判断是否满足某个条件
  52. if (currentScrollPosition > 0 && currentScrollPosition < maxScrollExtent) {
  53. print('Current scroll position: $currentScrollPosition');
  54. // 在这里添加你的条件判断逻辑
  55. }
  56. // 只有当上下滚动时才拦截通知
  57. if (isDownOrUp) {
  58. final tabsRouter = ref.watch(communityVmProvider).tabsRouter;
  59. }
  60. }
  61. return false;
  62. }
  63. @override
  64. Widget build(BuildContext context, WidgetRef ref) {
  65. final vm = ref.read(communityVmProvider.notifier);
  66. final state = ref.watch(communityVmProvider);
  67. final stateTabsRouter = ref.watch(communityVmProvider).tabsRouter;
  68. // final state = ref.watch(communityVmProvider);
  69. BuildContext contextPage = context;
  70. // 创建 GlobalKey
  71. final _tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  72. // useEffect((){
  73. // Log.d("CommunityPage initState");
  74. // return () {
  75. // Log.d("CommunityPage dispose");
  76. // };
  77. // },[]);
  78. return Scaffold(
  79. appBar: MyAppBar.appBar(
  80. context,
  81. "Community",
  82. backgroundColor: context.appColors.whiteBG,
  83. ),
  84. backgroundColor: context.appColors.backgroundDefault,
  85. body: NestedScrollView(
  86. headerSliverBuilder: (context, innerBoxIsScrolled) {
  87. return [
  88. SliverToBoxAdapter(
  89. child: Consumer(
  90. builder: (context, ref, _) {
  91. return _buildTopSection(context, ref, vm);
  92. },
  93. ),
  94. ),
  95. // SliverPersistentHeader(
  96. // pinned: true, // 吸顶
  97. // delegate: CustomSliverPersistentHeaderDelegate(
  98. // child: _buildTabsSection(contextPage, ref),
  99. // ),
  100. // ),
  101. ];
  102. },
  103. body: NotificationListener<ScrollNotification>(
  104. onNotification: (ScrollNotification notification) {
  105. // 是否拦截滚动 false 表示不拦截通知
  106. // return _isPreventScroll(notification, ref);
  107. return false;
  108. },
  109. child: AutoTabsRouter.pageView(
  110. key: _tabsRouterKey,
  111. routes: const [
  112. NewsPageRoute(),
  113. FollowingPageRoute(),
  114. ForyouPageRoute()
  115. ],
  116. builder: (context, child, pageController) {
  117. final tabsRouter = AutoTabsRouter.of(context);
  118. // Log.d("autotabsrouter build ${tabsRouter.activeIndex}");
  119. // Log.d("communityVmProvider.notifier state.tabsRouter.tabsRouter: ${ref.read(communityVmProvider.notifier).state.tabsRouter}");
  120. // ref.read(communityVmProvider.notifier).state.tabsRouter = tabsRouter;
  121. // ref.read(communityVmProvider.notifier).state.pageController = pageController;
  122. // Log.d("communityVmProvider.notifier state.tabsRouter.tabsRouter.activeIndex: ${ref.read(communityVmProvider.notifier).state.tabsRouter.activeIndex}");
  123. return Column(
  124. children: [
  125. // tab 组件
  126. _buildTabsSection(contextPage, ref, tabsRouter),
  127. // post 组件
  128. _buildPostSection(context, ref, vm),
  129. Expanded(
  130. child: child,
  131. ),
  132. ],
  133. );
  134. },
  135. ),
  136. ),
  137. )
  138. );
  139. }
  140. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm) {
  141. final topSectionsData = vm.topSectionsData;
  142. int curTagIdx = 0;
  143. return Container(
  144. color: Colors.white,
  145. padding: const EdgeInsets.only(top: 30, bottom: 30),
  146. child: Center(
  147. child: Row(
  148. mainAxisAlignment: MainAxisAlignment.center,
  149. crossAxisAlignment: CrossAxisAlignment.center,
  150. children: List.generate(topSectionsData.length, (index) {
  151. final item = topSectionsData[index];
  152. return Flexible(
  153. flex: 1,
  154. child: Column(
  155. children: [
  156. Container(
  157. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  158. height: 70,
  159. decoration: BoxDecoration(
  160. // color: currentTabIdx == index ? ColorUtils.string2Color('#E6F2FF') : Colors.white,
  161. shape: BoxShape.circle, // 设置为圆形
  162. boxShadow: index == curTagIdx ? [
  163. BoxShadow(
  164. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  165. blurRadius: 5, // 设置模糊半径
  166. spreadRadius: 0.05, // 控制阴影扩散
  167. offset: const Offset(0, 4), // 设置阴影偏移量
  168. ), ] : [],// 未选中时无阴影,
  169. ),
  170. child: MyAssetImage(
  171. item['icon'],
  172. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  173. height: 70,
  174. ).onTap(() {
  175. vm.handlerChangeCommunityType(context,index);
  176. },
  177. type: ClickType.throttle,
  178. ),
  179. ),
  180. SizedBox.fromSize(size: const Size(0, 9)),
  181. MyTextView(
  182. item['title'],
  183. fontSize: 15,
  184. textColor: index == curTagIdx ? ColorUtils.string2Color('#4161D0'):Colors.black,
  185. textAlign: TextAlign.center,
  186. isFontMedium: true,
  187. ),
  188. ],
  189. ),
  190. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  191. }),
  192. ),
  193. ),
  194. );
  195. }
  196. Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter){
  197. final vm = ref.read(communityVmProvider.notifier);
  198. final state = ref.watch(communityVmProvider);
  199. final tabsList = ref.watch(communityVmProvider.select((state) => state.tabsList));
  200. return Container(
  201. width: double.infinity,
  202. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  203. child: NewsfeedTabs(
  204. key: UniqueKey(),
  205. tabsList: tabsList!,
  206. tabsRouter: tabsRouter,
  207. onClickAction:(activeTabIdx){
  208. vm.handlerChangeTab(activeTabIdx, tabsRouter);
  209. }
  210. ),
  211. );
  212. }
  213. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm){
  214. return Container(
  215. height: 65.5,
  216. width: double.infinity,
  217. padding: const EdgeInsets.only(left: 20, right: 20),
  218. color: Colors.white,
  219. child: Row(
  220. children: [
  221. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  222. Expanded(
  223. child: Row(
  224. children: [
  225. Expanded(
  226. child: Container(
  227. // height: 65.5,
  228. // color: Colors.blue,
  229. child: MyTextView(
  230. "What’s on your mind?",
  231. textColor: ColorUtils.string2Color('#000000'),
  232. fontSize: 15,
  233. marginLeft: 15,
  234. alignment: Alignment.centerLeft,
  235. textAlign: TextAlign.left,
  236. backgroundColor: ColorUtils.string2Color('#ffffff'),
  237. maxLines: 1,
  238. isFontMedium: true,
  239. ),
  240. ),
  241. ),
  242. const MyAssetImage(
  243. Assets.communityCamera,
  244. width: 21,
  245. height: 16.5,
  246. ),
  247. ],
  248. ).onTap((){
  249. vm.handlerGotoPost(context);
  250. }),
  251. ),
  252. ],
  253. ),
  254. );
  255. }
  256. }