community_page.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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: NestedScrollView(
  78. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  79. return [
  80. // top 组件,转换为 Sliver 组件
  81. SliverToBoxAdapter(
  82. child: _buildTopSection(context, ref, vm, state),
  83. ),
  84. // tab 组件,使用 SliverPersistentHeader 实现吸顶
  85. // SliverPersistentHeader(
  86. // pinned: true,
  87. // delegate: CustomSliverPersistentHeaderDelegate(
  88. // child: _buildTabsSection(context, ref, tabsRouter, vm, state),
  89. // ),
  90. // ),
  91. // SliverToBoxAdapter(
  92. // child: _buildPostSection(context, ref, vm, state),
  93. // ),
  94. ];
  95. },
  96. body: Column(
  97. mainAxisSize: MainAxisSize.max,
  98. children: [
  99. Expanded(
  100. child: AutoTabsRouter.pageView(
  101. key: tabsRouterKey,
  102. routes: const [
  103. NewsPageRoute(),
  104. FollowingPageRoute(),
  105. ForyouPageRoute(),
  106. ForsalePageRoute(),
  107. ForrentPageRoute(),
  108. ],
  109. builder: (context, child, pageController) {
  110. final tabsRouter = AutoTabsRouter.of(context);
  111. return Column(
  112. children: [
  113. _buildTabsSection(context, ref, tabsRouter, vm, state),
  114. _buildPostSection(context, ref, vm, state),
  115. Expanded(child: child),
  116. ],
  117. );
  118. },
  119. )
  120. )
  121. ]
  122. )
  123. )
  124. // AutoTabsRouter.pageView(
  125. // key: tabsRouterKey,
  126. // routes: const [
  127. // NewsPageRoute(),
  128. // FollowingPageRoute(),
  129. // ForyouPageRoute(),
  130. // ForsalePageRoute(),
  131. // ForrentPageRoute(),
  132. // ],
  133. // // physics: const NeverScrollableScrollPhysics(), // 禁止滚动
  134. // builder: (context, child, pageController) {
  135. // final tabsRouter = AutoTabsRouter.of(context);
  136. //
  137. // return NestedScrollView(
  138. // headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  139. // return [
  140. // // top 组件,转换为 Sliver 组件
  141. // SliverToBoxAdapter(
  142. // child: _buildTopSection(context, ref, vm, state),
  143. // ),
  144. // // tab 组件,使用 SliverPersistentHeader 实现吸顶
  145. // // SliverPersistentHeader(
  146. // // pinned: true,
  147. // // delegate: CustomSliverPersistentHeaderDelegate(
  148. // // child: _buildTabsSection(context, ref, tabsRouter, vm, state),
  149. // // ),
  150. // // ),
  151. // // SliverToBoxAdapter(
  152. // // child: _buildPostSection(context, ref, vm, state),
  153. // // ),
  154. // ];
  155. // },
  156. // body: Column(
  157. // mainAxisSize: MainAxisSize.max,
  158. // children: [
  159. // _buildTabsSection(context, ref, tabsRouter, vm, state),
  160. // _buildPostSection(context, ref, vm, state),
  161. // Expanded(
  162. // child: child
  163. // ),
  164. // ],
  165. // ), // post 组件和其他内容
  166. // );
  167. // },
  168. // ),
  169. );
  170. }
  171. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  172. final topSectionsData = vm.topSectionsData;
  173. // final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  174. int curTagIdx = 0;
  175. int currentPageIdx = state.currentPageViewIdx;
  176. int newsfeedTabCount = state.newsFeedTabsList.length;
  177. if(currentPageIdx >= newsfeedTabCount){
  178. curTagIdx = 1;
  179. }else {
  180. curTagIdx = 0;
  181. }
  182. return Container(
  183. color: context.appColors.whiteBG,
  184. padding: const EdgeInsets.only(top: 23, bottom: 30),
  185. child: Center(
  186. child: Row(
  187. mainAxisSize: MainAxisSize.max,
  188. mainAxisAlignment: MainAxisAlignment.center,
  189. crossAxisAlignment: CrossAxisAlignment.center,
  190. children: List.generate(topSectionsData.length, (index) {
  191. final item = topSectionsData[index];
  192. return Column(
  193. children: [
  194. Container(
  195. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  196. height: 70,
  197. decoration: BoxDecoration(
  198. shape: BoxShape.circle, // 设置为圆形
  199. boxShadow: index == curTagIdx ? [
  200. BoxShadow(
  201. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  202. blurRadius: 5, // 设置模糊半径
  203. spreadRadius: 0.05, // 控制阴影扩散
  204. offset: const Offset(0, 4), // 设置阴影偏移量
  205. ),] : [],// 未选中时无阴影,
  206. ),
  207. child: MyAssetImage(
  208. item['icon'],
  209. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  210. height: 70,
  211. ).onTap(() {
  212. vm.handlerSwitchNewsfeedOrGaragesale(index, context, null);
  213. },
  214. type: ClickType.throttle,
  215. ),
  216. ),
  217. SizedBox.fromSize(size: const Size(0, 9)),
  218. MyTextView(
  219. item['title'],
  220. fontSize: 15,
  221. textColor: index == curTagIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  222. textAlign: TextAlign.center,
  223. isFontMedium: true,
  224. ),
  225. ],
  226. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  227. }),
  228. ),
  229. ),
  230. );
  231. }
  232. Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter, vm, state){
  233. int currentPageIndex = tabsRouter!.activeIndex ?? 0;
  234. int newsfeedTabCount = state.newsFeedTabsList.length;
  235. List<String> tabsList;
  236. if(currentPageIndex < newsfeedTabCount){
  237. // news feed
  238. tabsList = state.newsFeedTabsList ?? [];
  239. }else {
  240. tabsList = state.garageSaleTabsList ?? [];
  241. }
  242. return Container(
  243. width: double.infinity,
  244. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 12),
  245. color: ColorUtils.string2Color('#F2F3F6'),
  246. child: NewsfeedTabs(
  247. key: UniqueKey(),
  248. tabsList: tabsList,
  249. tabsRouter: tabsRouter,
  250. onClickAction:(Map<String, dynamic>? params){
  251. if (params != null) {
  252. // 解构 params
  253. final int? currentCatgoryIdx = params['currentCatgoryIdx'] as int?;
  254. final int? tabIdx = params['tabIdx'] as int?;
  255. vm.handlerChangeTab(tabIdx, tabsRouter, currentCatgoryIdx);
  256. }
  257. }
  258. ),
  259. );
  260. }
  261. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm, state){
  262. int currentPageIndex = state.currentPageViewIdx;
  263. int newsfeedTabCount = state.newsFeedTabsList.length;
  264. if(currentPageIndex < newsfeedTabCount){
  265. // news feed
  266. return _buildNewsFeedPost(context, ref, vm, state);
  267. }else {
  268. return _buildGarageSalePost(context, ref, vm, state);
  269. }
  270. }
  271. Widget _buildNewsFeedPost(BuildContext context, WidgetRef ref, vm, state){
  272. return Container(
  273. height: 65.5,
  274. width: double.infinity,
  275. padding: const EdgeInsets.only(left: 20, right: 20),
  276. color: Colors.white,
  277. child: Row(
  278. children: [
  279. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  280. Expanded(
  281. child: MyTextView(
  282. "What’s on your mind?",
  283. textColor: context.appColors.textBlack,
  284. fontSize: 15,
  285. marginLeft: 15,
  286. alignment: Alignment.centerLeft,
  287. textAlign: TextAlign.left,
  288. backgroundColor: ColorUtils.string2Color('#ffffff'),
  289. maxLines: 1,
  290. isFontMedium: true,
  291. ),
  292. ),
  293. const MyAssetImage(
  294. Assets.communityCamera,
  295. width: 21,
  296. height: 16.5,
  297. ),
  298. ],
  299. ).onTap((){
  300. vm.handlerGotoNewsfeedPost(context);
  301. }),
  302. );
  303. }
  304. Widget _buildGarageSalePost(BuildContext context, WidgetRef ref, vm, state){
  305. return Container(
  306. height: 65.5,
  307. width: double.infinity,
  308. padding: const EdgeInsets.only(left: 20, right: 20),
  309. color: Colors.white,
  310. child: Row(
  311. children: [
  312. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  313. Expanded(
  314. child: MyTextView(
  315. "Sell Item",
  316. textColor: context.appColors.textBlack,
  317. fontSize: 15,
  318. marginLeft: 15,
  319. alignment: Alignment.centerLeft,
  320. textAlign: TextAlign.left,
  321. backgroundColor: ColorUtils.string2Color('#ffffff'),
  322. maxLines: 1,
  323. isFontMedium: true,
  324. ),
  325. ),
  326. const MyAssetImage(
  327. Assets.communityCamera,
  328. width: 21,
  329. height: 16.5,
  330. ),
  331. ],
  332. ).onTap((){
  333. vm.handlerGotoGaragePost(context);
  334. }),
  335. );
  336. }
  337. }