community_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 '../my_posts/my_posts_page.dart';
  18. import 'newsfeed_tabs.dart';
  19. import 'community_vm.dart';
  20. import 'customSilverHeaderTabs.dart';
  21. final tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  22. @RoutePage()
  23. class CommunityPage extends HookConsumerWidget {
  24. const CommunityPage({Key? key}) : super(key: key);
  25. //启动当前页面
  26. static void startInstance({BuildContext? context}) {
  27. if (context != null) {
  28. context.router.push(const CommunityPageRoute());
  29. } else {
  30. appRouter.push(const CommunityPageRoute());
  31. }
  32. }
  33. @override
  34. Widget build(BuildContext context, WidgetRef ref) {
  35. final vm = ref.read(communityVmProvider.notifier);
  36. final state = ref.watch(communityVmProvider);
  37. useEffect((){
  38. Log.d("CommunityPage initState");
  39. // 延迟监听
  40. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  41. if(tabsRouterKey.currentState?.controller != null){
  42. tabsRouterKey.currentState?.controller?.addListener((){
  43. vm.tabsRouterChange();
  44. });
  45. }
  46. });
  47. return (){
  48. Log.d("CommunityPage dispose");
  49. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  50. };
  51. },[]);
  52. return Scaffold(
  53. appBar: MyAppBar.searchAppBar(
  54. context,
  55. actions: [
  56. const MyAssetImage(
  57. Assets.communityLikeActive,
  58. width: 21.5,
  59. height: 21.5,
  60. ).onTap((){
  61. vm.handlerClickNavbarLikeBtn(context);
  62. }),
  63. SizedBox(width: state.currentCategoryIdx ==0 ? 15:20),
  64. state.currentCategoryIdx ==1 ?
  65. const MyAssetImage(
  66. Assets.communityFillterIcon,
  67. width: 21,
  68. height: 21,
  69. ).onTap((){
  70. vm.handlerClickNavbarFilterBtn(context);
  71. }) : const SizedBox.shrink(),
  72. const SizedBox(width: 15),
  73. ],
  74. backgroundColor: context.appColors.backgroundWhite,
  75. ),
  76. backgroundColor: context.appColors.backgroundDefault,
  77. body: AutoTabsRouter.pageView(
  78. key: tabsRouterKey,
  79. routes: const [
  80. NewsPageRoute(),
  81. FollowingPageRoute(),
  82. ForyouPageRoute(),
  83. ForsalePageRoute(),
  84. ForrentPageRoute(),
  85. ],
  86. // physics: const NeverScrollableScrollPhysics(), // 禁止滚动
  87. builder: (context, child, pageController) {
  88. final tabsRouter = AutoTabsRouter.of(context);
  89. return NestedScrollView(
  90. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  91. return [
  92. // top 组件,转换为 Sliver 组件
  93. SliverToBoxAdapter(
  94. child: _buildTopSection(context, ref, vm, state),
  95. ),
  96. // tab 组件,使用 SliverPersistentHeader 实现吸顶
  97. // SliverPersistentHeader(
  98. // pinned: true,
  99. // delegate: CustomSliverPersistentHeaderDelegate(
  100. // child: _buildTabsSection(context, ref, tabsRouter, vm, state),
  101. // ),
  102. // ),
  103. // SliverToBoxAdapter(
  104. // child: _buildPostSection(context, ref, vm, state),
  105. // ),
  106. ];
  107. },
  108. body: Column(
  109. mainAxisSize: MainAxisSize.max,
  110. children: [
  111. _buildTabsSection(context, ref, tabsRouter, vm, state),
  112. _buildPostSection(context, ref, vm, state),
  113. Expanded(
  114. child: child
  115. ),
  116. ],
  117. ), // post 组件和其他内容
  118. );
  119. },
  120. ),
  121. );
  122. }
  123. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  124. final topSectionsData = vm.topSectionsData;
  125. // final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  126. int curTagIdx = 0;
  127. int currentPageIdx = state.currentPageViewIdx;
  128. int newsfeedTabCount = state.newsFeedTabsList.length;
  129. if(currentPageIdx >= newsfeedTabCount){
  130. curTagIdx = 1;
  131. }else {
  132. curTagIdx = 0;
  133. }
  134. return Container(
  135. color: context.appColors.whiteBG,
  136. padding: const EdgeInsets.only(top: 23, bottom: 30),
  137. child: Center(
  138. child: Row(
  139. mainAxisSize: MainAxisSize.max,
  140. mainAxisAlignment: MainAxisAlignment.center,
  141. crossAxisAlignment: CrossAxisAlignment.center,
  142. children: List.generate(topSectionsData.length, (index) {
  143. final item = topSectionsData[index];
  144. return Column(
  145. children: [
  146. Container(
  147. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  148. height: 70,
  149. decoration: BoxDecoration(
  150. shape: BoxShape.circle, // 设置为圆形
  151. boxShadow: index == curTagIdx ? [
  152. BoxShadow(
  153. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  154. blurRadius: 5, // 设置模糊半径
  155. spreadRadius: 0.05, // 控制阴影扩散
  156. offset: const Offset(0, 4), // 设置阴影偏移量
  157. ),] : [],// 未选中时无阴影,
  158. ),
  159. child: MyAssetImage(
  160. item['icon'],
  161. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  162. height: 70,
  163. ).onTap(() {
  164. vm.handlerSwitchNewsfeedOrGaragesale(index, context, null);
  165. },
  166. type: ClickType.throttle,
  167. ),
  168. ),
  169. SizedBox.fromSize(size: const Size(0, 9)),
  170. MyTextView(
  171. item['title'],
  172. fontSize: 15,
  173. textColor: index == curTagIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  174. textAlign: TextAlign.center,
  175. isFontMedium: true,
  176. ),
  177. ],
  178. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  179. }),
  180. ),
  181. ),
  182. );
  183. }
  184. Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter, vm, state){
  185. int currentPageIndex = tabsRouter!.activeIndex ?? 0;
  186. int newsfeedTabCount = state.newsFeedTabsList.length;
  187. List<String> tabsList;
  188. if(currentPageIndex < newsfeedTabCount){
  189. // news feed
  190. tabsList = state.newsFeedTabsList ?? [];
  191. }else {
  192. tabsList = state.garageSaleTabsList ?? [];
  193. }
  194. return Container(
  195. width: double.infinity,
  196. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 12),
  197. color: ColorUtils.string2Color('#F2F3F6'),
  198. child: NewsfeedTabs(
  199. key: UniqueKey(),
  200. tabsList: tabsList,
  201. tabsRouter: tabsRouter,
  202. onClickAction:(Map<String, dynamic>? params){
  203. if (params != null) {
  204. // 解构 params
  205. final int? currentCatgoryIdx = params['currentCatgoryIdx'] as int?;
  206. final int? tabIdx = params['tabIdx'] as int?;
  207. vm.handlerChangeTab(tabIdx, tabsRouter, currentCatgoryIdx);
  208. }
  209. }
  210. ),
  211. );
  212. }
  213. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm, state){
  214. int currentPageIndex = state.currentPageViewIdx;
  215. int newsfeedTabCount = state.newsFeedTabsList.length;
  216. if(currentPageIndex < newsfeedTabCount){
  217. // news feed
  218. return _buildNewsFeedPost(context, ref, vm, state);
  219. }else {
  220. return _buildGarageSalePost(context, ref, vm, state);
  221. }
  222. }
  223. Widget _buildNewsFeedPost(BuildContext context, WidgetRef ref, vm, state){
  224. return Container(
  225. height: 65.5,
  226. width: double.infinity,
  227. padding: const EdgeInsets.only(left: 20, right: 20),
  228. color: Colors.white,
  229. child: Row(
  230. children: [
  231. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  232. Expanded(
  233. child: MyTextView(
  234. "What’s on your mind?",
  235. textColor: context.appColors.textBlack,
  236. fontSize: 15,
  237. marginLeft: 15,
  238. alignment: Alignment.centerLeft,
  239. textAlign: TextAlign.left,
  240. backgroundColor: ColorUtils.string2Color('#ffffff'),
  241. maxLines: 1,
  242. isFontMedium: true,
  243. ),
  244. ),
  245. const MyAssetImage(
  246. Assets.communityCamera,
  247. width: 21,
  248. height: 16.5,
  249. ),
  250. ],
  251. ).onTap((){
  252. vm.handlerGotoNewsfeedPost(context);
  253. }),
  254. );
  255. }
  256. Widget _buildGarageSalePost(BuildContext context, WidgetRef ref, vm, state){
  257. return Container(
  258. height: 65.5,
  259. width: double.infinity,
  260. padding: const EdgeInsets.only(left: 20, right: 20),
  261. color: Colors.white,
  262. child: Row(
  263. children: [
  264. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  265. Expanded(
  266. child: MyTextView(
  267. "Sell Item",
  268. textColor: context.appColors.textBlack,
  269. fontSize: 15,
  270. marginLeft: 15,
  271. alignment: Alignment.centerLeft,
  272. textAlign: TextAlign.left,
  273. backgroundColor: ColorUtils.string2Color('#ffffff'),
  274. maxLines: 1,
  275. isFontMedium: true,
  276. ),
  277. ),
  278. const MyAssetImage(
  279. Assets.communityCamera,
  280. width: 21,
  281. height: 16.5,
  282. ),
  283. ],
  284. ).onTap((){
  285. vm.handlerGotoGaragePost(context);
  286. }),
  287. );
  288. }
  289. }