community_page.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/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. @override
  32. Widget build(BuildContext context, WidgetRef ref) {
  33. final vm = ref.read(communityVmProvider.notifier);
  34. final stateTabsRouter = ref.watch(communityVmProvider).tabsRouter;
  35. // final state = ref.watch(communityVmProvider);
  36. BuildContext contextPage = context;
  37. // useEffect((){
  38. // Log.d("CommunityPage initState");
  39. // return () {
  40. // Log.d("CommunityPage dispose");
  41. // };
  42. // },[]);
  43. return Scaffold(
  44. appBar: MyAppBar.appBar(
  45. context,
  46. "Community",
  47. backgroundColor: context.appColors.whiteBG,
  48. ),
  49. backgroundColor: context.appColors.backgroundDefault,
  50. body: NestedScrollView(
  51. headerSliverBuilder: (context, innerBoxIsScrolled) {
  52. return [
  53. SliverToBoxAdapter(
  54. child: Consumer(
  55. builder: (context, ref, _) {
  56. final tabsRouter = ref.watch(communityVmProvider).tabsRouter;
  57. // final vm = ref.read(communityVmProvider.notifier);
  58. if(tabsRouter != null){
  59. return _buildTopSection(context, ref, vm, tabsRouter);
  60. }else {
  61. return const SizedBox.shrink();
  62. }
  63. },
  64. ),
  65. ),
  66. // SliverPersistentHeader(
  67. // pinned: true, // 吸顶
  68. // delegate: CustomSliverPersistentHeaderDelegate(
  69. // child: _buildTabsSection(contextPage, ref),
  70. // ),
  71. // ),
  72. ];
  73. },
  74. body: AutoTabsRouter.pageView(
  75. routes: const [
  76. NewsPageRoute(),
  77. FollowingPageRoute(),
  78. ForyouPageRoute()
  79. ],
  80. builder: (context, child, pageController) {
  81. final tabsRouter = AutoTabsRouter.of(context);
  82. Log.d("autotabsRouter ---build ${tabsRouter.activeIndex}");
  83. // 将tabsRouter 放入 CommunityVmProvider 中
  84. // if(stateTabsRouter == null){
  85. // vm.setTabsRouterAndPageController(tabsRouter, pageController);
  86. // }else {
  87. //
  88. // }
  89. vm.setTabsRouterAndPageController(tabsRouter, pageController);
  90. return Column(
  91. children: [
  92. // Text("${vm.state.activeTabIdx}"),
  93. // _buildTabsSection(contextPage, ref, tabsRouter),
  94. // tabs 组件
  95. // Visibility(
  96. // visible: true,
  97. // child: _buildTabsSection(contextPage, ref, tabsRouter),
  98. // ),
  99. // post 组件
  100. // _buildPostSection(context, ref, vm),
  101. Expanded(
  102. child: child,
  103. ),
  104. ],
  105. );
  106. },
  107. ),
  108. )
  109. );
  110. }
  111. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, tabsRouter) {
  112. final topSectionsData = vm.topSectionsData;
  113. final currentTabIdx = tabsRouter.activeIndex;
  114. return Container(
  115. color: Colors.white,
  116. padding: const EdgeInsets.only(top: 30, bottom: 30),
  117. child: Center(
  118. child: Row(
  119. mainAxisAlignment: MainAxisAlignment.center,
  120. crossAxisAlignment: CrossAxisAlignment.center,
  121. children: List.generate(topSectionsData.length, (index) {
  122. final item = topSectionsData[index];
  123. return Flexible(
  124. flex: 1,
  125. child: Column(
  126. children: [
  127. Container(
  128. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  129. height: 70,
  130. decoration: BoxDecoration(
  131. // color: currentTabIdx == index ? ColorUtils.string2Color('#E6F2FF') : Colors.white,
  132. shape: BoxShape.circle, // 设置为圆形
  133. boxShadow: index == 0 ? [
  134. BoxShadow(
  135. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  136. blurRadius: 5, // 设置模糊半径
  137. spreadRadius: 0.05, // 控制阴影扩散
  138. offset: const Offset(0, 4), // 设置阴影偏移量
  139. ), ] : [],// 未选中时无阴影,
  140. ),
  141. child: MyAssetImage(
  142. item['icon'],
  143. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  144. height: 70,
  145. ).onTap(() {
  146. vm.handlerChangeCommunityType(context,index);
  147. },
  148. type: ClickType.throttle,
  149. ),
  150. ),
  151. SizedBox.fromSize(size: const Size(0, 9)),
  152. MyTextView(
  153. item['title'],
  154. fontSize: 15,
  155. textColor: index ==0 ? ColorUtils.string2Color('#4161D0'):Colors.black,
  156. textAlign: TextAlign.center,
  157. isFontMedium: true,
  158. ),
  159. ],
  160. ),
  161. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  162. }),
  163. ),
  164. ),
  165. );
  166. }
  167. Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter){
  168. final vm = ref.read(communityVmProvider.notifier);
  169. final tabsList = ref.watch(communityVmProvider.select((state) => state.tabsList));
  170. return Container(
  171. width: double.infinity,
  172. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  173. child: NewsfeedTabs(
  174. key: UniqueKey(),
  175. tabsList: tabsList!,
  176. onClickAction:(activeTabIdx){
  177. print('点击了tab ${activeTabIdx}');
  178. // vm.handlerChangeTab(activeTabIdx);
  179. // tabsRouter.setActiveIndex(activeTabIdx);
  180. }
  181. ),
  182. );
  183. }
  184. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm){
  185. return Container(
  186. height: 65.5,
  187. width: double.infinity,
  188. padding: const EdgeInsets.only(left: 20, right: 20),
  189. color: Colors.white,
  190. child: Row(
  191. children: [
  192. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  193. Expanded(
  194. child: Row(
  195. children: [
  196. Expanded(
  197. child: Container(
  198. // height: 65.5,
  199. // color: Colors.blue,
  200. child: MyTextView(
  201. "What’s on your mind?",
  202. textColor: ColorUtils.string2Color('#000000'),
  203. fontSize: 15,
  204. marginLeft: 15,
  205. alignment: Alignment.centerLeft,
  206. textAlign: TextAlign.left,
  207. backgroundColor: ColorUtils.string2Color('#ffffff'),
  208. maxLines: 1,
  209. isFontMedium: true,
  210. ),
  211. ),
  212. ),
  213. const MyAssetImage(
  214. Assets.communityCamera,
  215. width: 21,
  216. height: 16.5,
  217. ),
  218. ],
  219. ).onTap((){
  220. vm.handlerGotoPost(context);
  221. }),
  222. ),
  223. ],
  224. ),
  225. );
  226. }
  227. }