community_page.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:cs_resources/generated/l10n.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter_hooks/flutter_hooks.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/color_utils.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/my_load_image.dart';
  13. import 'package:widgets/ext/ex_widget.dart';
  14. import 'package:widgets/my_text_view.dart';
  15. import 'package:widgets/my_appbar.dart';
  16. import 'package:cs_resources/theme/app_colors_theme.dart';
  17. import 'package:widgets/widget_export.dart';
  18. import '../../router/page/community_page_router.dart';
  19. import '../my_posts/my_posts_page.dart';
  20. import 'newsfeed_tabs.dart';
  21. import 'community_vm.dart';
  22. import 'customSilverHeaderTabs.dart';
  23. final tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  24. final GlobalKey<ExtendedNestedScrollViewState> extendedNestedScrollViewKey =
  25. GlobalKey<ExtendedNestedScrollViewState>();
  26. @RoutePage()
  27. class CommunityPage extends HookConsumerWidget with WidgetsBindingObserver {
  28. CommunityPage({Key? key}) : super(key: key);
  29. //启动当前页面
  30. static void startInstance({BuildContext? context}) {
  31. if (context != null) {
  32. context.router.push(const CommunityPageRoute());
  33. } else {
  34. appRouter.push(const CommunityPageRoute());
  35. }
  36. }
  37. bool _isKeyboardVisible = false;
  38. void handlerNestedScrollViewScroll({double? outerOffset, double? innerOffset, bool? isOuterScrollAnimated=false, bool? isInnerScrollAnimated=false}){
  39. if(outerOffset !=null){
  40. if(isOuterScrollAnimated!){
  41. extendedNestedScrollViewKey.currentState?.outerController.animateTo(
  42. outerOffset,
  43. duration: const Duration(seconds: 1),
  44. curve: Curves.easeIn,
  45. );
  46. }else {
  47. extendedNestedScrollViewKey.currentState?.outerController.jumpTo(
  48. outerOffset,
  49. );
  50. }
  51. }
  52. if(innerOffset !=null){
  53. extendedNestedScrollViewKey.currentState?.innerPositions.forEach((position) {
  54. if(isInnerScrollAnimated!){
  55. position.animateTo(innerOffset,
  56. duration: Duration(seconds: 1), curve: Curves.easeIn);
  57. }else {
  58. position.jumpTo(innerOffset);
  59. }
  60. });
  61. }
  62. }
  63. @override
  64. void didChangeMetrics() {
  65. final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom;
  66. final newValue = bottomInset > 0.0;
  67. if (_isKeyboardVisible != newValue) {
  68. _isKeyboardVisible = newValue;
  69. if (_isKeyboardVisible) {
  70. handlerNestedScrollViewScroll(innerOffset: 0.0,);
  71. print("键盘已显示");
  72. } else {
  73. WidgetsBinding.instance.removeObserver(this);
  74. print("键盘已隐藏");
  75. }
  76. }
  77. }
  78. @override
  79. Widget build(BuildContext context, WidgetRef ref) {
  80. final vm = ref.read(communityVmProvider.notifier);
  81. final state = ref.watch(communityVmProvider);
  82. useEffect(() {
  83. // 监听窗口
  84. WidgetsBinding.instance.addObserver(this);
  85. }, []);
  86. useEffect((){
  87. Log.d("CommunityPage initState");
  88. // 延迟监听
  89. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  90. if(tabsRouterKey.currentState?.controller != null){
  91. tabsRouterKey.currentState?.controller?.addListener((){
  92. vm.tabsRouterChange();
  93. });
  94. }
  95. });
  96. return (){
  97. Log.d("CommunityPage dispose");
  98. WidgetsBinding.instance.removeObserver(this);
  99. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  100. };
  101. },[]);
  102. return Scaffold(
  103. appBar: MyAppBar.searchAppBar(
  104. context,
  105. value: vm.getCurrentQueryParams('keyword'),
  106. actions: [
  107. const MyAssetImage(
  108. Assets.communityLikeActive,
  109. width: 21.5,
  110. height: 21.5,
  111. ).onTap((){
  112. vm.handlerClickNavbarLikeBtn(context);
  113. }),
  114. SizedBox(width: state.currentCategoryIdx ==0 ? 15:20),
  115. state.currentCategoryIdx ==1 ?
  116. const MyAssetImage(
  117. Assets.communityFillterIcon,
  118. width: 21,
  119. height: 21,
  120. ).onTap((){
  121. vm.handlerClickNavbarFilterBtn(context);
  122. }) : const SizedBox.shrink(),
  123. const SizedBox(width: 15),
  124. ],
  125. backgroundColor: context.appColors.backgroundWhite,
  126. onSearch: (value) {
  127. vm.handlerSearch(value);
  128. }
  129. ),
  130. backgroundColor: context.appColors.backgroundDefault,
  131. body: ExtendedNestedScrollView(
  132. key: extendedNestedScrollViewKey,
  133. onlyOneScrollInBody: true,
  134. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  135. return [
  136. // SliverPersistentHeader(
  137. // delegate: CustomSliverPersistentHeaderDelegate(
  138. // maxHeight: 180,
  139. // minHeight: 180,
  140. // child: _buildTopSection(context, ref, vm, state),
  141. // ),
  142. // pinned: false,
  143. // ),
  144. // top 组件,转换为 Sliver 组件
  145. SliverToBoxAdapter(
  146. child: _buildTopSection(context, ref, vm, state),
  147. ),
  148. ];
  149. },
  150. body: Column(
  151. mainAxisSize: MainAxisSize.max,
  152. children: [
  153. Expanded(
  154. child: AutoTabsRouter.pageView(
  155. key: tabsRouterKey,
  156. routes: const [
  157. NewsPageRoute(),
  158. FollowingPageRoute(),
  159. ForyouPageRoute(),
  160. ForsalePageRoute(),
  161. ForrentPageRoute(),
  162. ],
  163. builder: (context, child, pageController) {
  164. final tabsRouter = AutoTabsRouter.of(context);
  165. return Column(
  166. children: [
  167. _buildTabsSection(context, ref, tabsRouter, vm, state),
  168. _buildPostSection(context, ref, vm, state),
  169. Expanded(
  170. child: child
  171. ),
  172. ],
  173. );
  174. },
  175. )
  176. )
  177. ]
  178. )
  179. )
  180. );
  181. }
  182. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  183. final topSectionsData = vm.topSectionsData;
  184. // final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  185. int curTagIdx = 0;
  186. int currentPageIdx = state.currentPageViewIdx;
  187. int newsfeedTabCount = state.newsFeedTabsList.length;
  188. if(currentPageIdx >= newsfeedTabCount){
  189. curTagIdx = 1;
  190. }else {
  191. curTagIdx = 0;
  192. }
  193. return Container(
  194. color: context.appColors.whiteBG,
  195. padding: const EdgeInsets.only(top: 0, bottom: 10),
  196. child: Center(
  197. child: Row(
  198. mainAxisSize: MainAxisSize.max,
  199. mainAxisAlignment: MainAxisAlignment.center,
  200. crossAxisAlignment: CrossAxisAlignment.center,
  201. children: List.generate(topSectionsData.length, (index) {
  202. final item = topSectionsData[index];
  203. return Column(
  204. children: [
  205. Container(
  206. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  207. height: 70,
  208. decoration: BoxDecoration(
  209. shape: BoxShape.circle, // 设置为圆形
  210. color: ColorUtils.string2Color("#F0F8FF"),
  211. boxShadow: index == curTagIdx ? [
  212. BoxShadow(
  213. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  214. blurRadius: 5, // 设置模糊半径
  215. spreadRadius: 0.05, // 控制阴影扩散
  216. offset: const Offset(0, 4), // 设置阴影偏移量
  217. ),] : [],// 未选中时无阴影,
  218. ),
  219. child: MyAssetImage(
  220. item['icon'],
  221. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  222. // width: 70,
  223. height: 70,
  224. ).onTap(() {
  225. vm.handlerSwitchNewsfeedOrGaragesale(index, context, null);
  226. },
  227. type: ClickType.throttle,
  228. ),
  229. ),
  230. SizedBox.fromSize(size: const Size(0, 9)),
  231. MyTextView(
  232. item['title'],
  233. fontSize: 15,
  234. textColor: index == curTagIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  235. textAlign: TextAlign.center,
  236. isFontMedium: true,
  237. ),
  238. ],
  239. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  240. }),
  241. ),
  242. ),
  243. );
  244. }
  245. Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter, vm, state){
  246. int currentPageIndex = tabsRouter!.activeIndex ?? 0;
  247. int newsfeedTabCount = state.newsFeedTabsList.length;
  248. List<String> tabsList;
  249. if(currentPageIndex < newsfeedTabCount){
  250. // news feed
  251. tabsList = state.newsFeedTabsList ?? [];
  252. }else {
  253. tabsList = state.garageSaleTabsList ?? [];
  254. }
  255. return Container(
  256. width: double.infinity,
  257. color: ColorUtils.string2Color('#F2F3F6'),
  258. child: Center(
  259. child: NewsfeedTabs(
  260. key: UniqueKey(),
  261. tabsList: tabsList,
  262. tabsRouter: tabsRouter,
  263. margin: EdgeInsets.only(top: 14,bottom: 12, left: 20, right: 20),
  264. onClickAction:(Map<String, dynamic>? params){
  265. if (params != null) {
  266. // 解构 params
  267. final int? currentCatgoryIdx = params['currentCatgoryIdx'] as int?;
  268. final int? tabIdx = params['tabIdx'] as int?;
  269. vm.handlerChangeTab(tabIdx, tabsRouter, currentCatgoryIdx);
  270. }
  271. }
  272. ),
  273. ),
  274. );
  275. }
  276. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm, state){
  277. int currentPageIndex = state.currentPageViewIdx;
  278. int newsfeedTabCount = state.newsFeedTabsList.length;
  279. if(currentPageIndex < newsfeedTabCount){
  280. // news feed
  281. return _buildNewsFeedPost(context, ref, vm, state);
  282. }else {
  283. return _buildGarageSalePost(context, ref, vm, state);
  284. }
  285. }
  286. Widget _buildNewsFeedPost(BuildContext context, WidgetRef ref, vm, state){
  287. final userConfig = UserConfigService.getState(ref: ref);
  288. return Container(
  289. height: 65.5,
  290. width: double.infinity,
  291. padding: const EdgeInsets.only(left: 20, right: 20),
  292. color: Colors.white,
  293. child: Row(
  294. children: [
  295. // const MyAssetImage(Assets.communityNewsFeed, width: 45,height: 45,),
  296. MyLoadImage(
  297. userConfig.user?.avatar,
  298. width: 45,
  299. height: 45,
  300. isCircle: true,
  301. fit: BoxFit.fill,
  302. ),
  303. Expanded(
  304. child: MyTextView(
  305. S.current.what_on_your_mind,
  306. textColor: context.appColors.textBlack,
  307. fontSize: 15,
  308. marginLeft: 15,
  309. alignment: Alignment.centerLeft,
  310. textAlign: TextAlign.left,
  311. backgroundColor: ColorUtils.string2Color('#ffffff'),
  312. maxLines: 1,
  313. isFontMedium: true,
  314. ),
  315. ),
  316. const MyAssetImage(
  317. Assets.communityCamera,
  318. width: 21,
  319. height: 16.5,
  320. ),
  321. ],
  322. ).onTap((){
  323. vm.handlerGotoNewsfeedPost(context);
  324. }),
  325. );
  326. }
  327. Widget _buildGarageSalePost(BuildContext context, WidgetRef ref, vm, state){
  328. final userConfig = UserConfigService.getState(ref: ref);
  329. return Container(
  330. height: 65.5,
  331. width: double.infinity,
  332. padding: const EdgeInsets.only(left: 20, right: 20),
  333. color: Colors.white,
  334. child: Row(
  335. children: [
  336. MyLoadImage(
  337. userConfig.user?.avatar,
  338. width: 45,
  339. height: 45,
  340. isCircle: true,
  341. fit: BoxFit.fill,
  342. ),
  343. Expanded(
  344. child: MyTextView(
  345. S.current.sell_item,
  346. textColor: context.appColors.textBlack,
  347. fontSize: 15,
  348. marginLeft: 15,
  349. alignment: Alignment.centerLeft,
  350. textAlign: TextAlign.left,
  351. backgroundColor: ColorUtils.string2Color('#ffffff'),
  352. maxLines: 1,
  353. isFontMedium: true,
  354. ),
  355. ),
  356. const MyAssetImage(
  357. Assets.communityCamera,
  358. width: 21,
  359. height: 16.5,
  360. ),
  361. ],
  362. ).onTap((){
  363. vm.handlerGotoGaragePost(context);
  364. }),
  365. );
  366. }
  367. }