my_posts_page.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:cpt_community/modules/my_posts/my_posts_vm.dart';
  2. import 'package:cpt_community/router/page/community_page_router.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:auto_route/auto_route.dart';
  6. import 'package:flutter_hooks/flutter_hooks.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:router/ext/auto_router_extensions.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:widgets/my_appbar.dart';
  11. import '../my_posts/my_posts_forrent/my_posts_forrent_page.dart';
  12. import '../my_posts/my_posts_forsale/my_posts_forsale_page.dart';
  13. import '../my_posts/my_posts_newsfeed/my_posts_newsfeed_page.dart';
  14. import 'my_posts_tabs.dart';
  15. final myPostsPageTabsRouterKey = GlobalKey<AutoTabsRouterState>();
  16. @RoutePage()
  17. class MyPostsPage extends HookConsumerWidget {
  18. const MyPostsPage({Key? key}) : super(key: key);
  19. //启动当前页面
  20. static void startInstance({BuildContext? context}) {
  21. if (context != null) {
  22. context.router.push(const MyPostsPageRoute());
  23. } else {
  24. appRouter.push(const MyPostsPageRoute());
  25. }
  26. }
  27. @override
  28. Widget build(BuildContext context, WidgetRef ref) {
  29. BuildContext pageContext = context;
  30. final vm = ref.watch(myPostsVmProvider.notifier);
  31. final state = ref.watch(myPostsVmProvider);
  32. useEffect((){
  33. Log.d("mypost page initState");
  34. // 延迟监听
  35. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  36. if(myPostsPageTabsRouterKey.currentState?.controller != null){
  37. myPostsPageTabsRouterKey.currentState?.controller?.addListener((){
  38. vm.tabsRouterChange();
  39. });
  40. }
  41. });
  42. return (){
  43. myPostsPageTabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  44. };
  45. },[]);
  46. // // 创建 GlobalKey
  47. // final GlobalKey<AutoTabsRouterState> _tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  48. return Scaffold(
  49. appBar: MyAppBar.appBar(
  50. context,
  51. "My Posts",
  52. backgroundColor: context.appColors.backgroundWhite,
  53. ),
  54. backgroundColor: context.appColors.backgroundDefault,
  55. body: Column(
  56. children: [
  57. Expanded(
  58. child: AutoTabsRouter.pageView(
  59. key: myPostsPageTabsRouterKey,
  60. routes: const [
  61. MyPostsNewsfeedPageRoute(),
  62. MyPostsForSalePageRoute(),
  63. MyPostsForRentPageRoute()
  64. ],
  65. builder: (context, child, pageController) {
  66. final tabsRouter = AutoTabsRouter.of(context);
  67. return Column(
  68. children: [
  69. // tab 标签
  70. _buildHeaderTabs(context, ref, state),
  71. Expanded(
  72. child: child,
  73. )
  74. ],
  75. );
  76. },
  77. )
  78. )
  79. ],
  80. ),
  81. );
  82. }
  83. Widget _buildHeaderTabs(BuildContext context, WidgetRef ref, state) {
  84. final vm = ref.read(myPostsVmProvider.notifier);
  85. return Container(
  86. color: context.appColors.textWhite,
  87. child: MyPostsTab(
  88. key: UniqueKey(),
  89. tabsList: state.tabsList,
  90. tabsRouter: null,
  91. onClickAction:(activeTabIdx){
  92. Log.d("点击的tab index: $activeTabIdx");
  93. vm.handlerChangeTab(activeTabIdx, null);
  94. },
  95. ),
  96. );
  97. }
  98. }