my_posts_page.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/my_appbar.dart';
  10. import '../my_posts/my_posts_forrent/my_posts_forrent_page.dart';
  11. import '../my_posts/my_posts_forsale/my_posts_forsale_page.dart';
  12. import '../my_posts/my_posts_newsfeed/my_posts_newsfeed_page.dart';
  13. import 'my_posts_tabs.dart';
  14. @RoutePage()
  15. class MyPostsPage extends HookConsumerWidget {
  16. const MyPostsPage({Key? key}) : super(key: key);
  17. //启动当前页面
  18. static void startInstance({BuildContext? context}) {
  19. if (context != null) {
  20. context.router.push(const MyPostsPageRoute());
  21. } else {
  22. appRouter.push(const MyPostsPageRoute());
  23. }
  24. }
  25. @override
  26. Widget build(BuildContext context, WidgetRef ref) {
  27. BuildContext pageContext = context;
  28. final vm = ref.watch(myPostsVmProvider.notifier);
  29. final state = ref.watch(myPostsVmProvider);
  30. var _pageController = null;
  31. // // 创建 GlobalKey
  32. // final GlobalKey<AutoTabsRouterState> _tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  33. return Scaffold(
  34. appBar: MyAppBar.appBar(
  35. context,
  36. "My Posts",
  37. backgroundColor: context.appColors.whiteBG,
  38. ),
  39. backgroundColor: context.appColors.backgroundDefault,
  40. body: Column(
  41. children: [
  42. Expanded(
  43. child: AutoTabsRouter.pageView(
  44. key: UniqueKey(),
  45. routes: const [
  46. MyPostsNewsfeedPageRoute(),
  47. MyPostsForSalePageRoute(),
  48. MyPostsForRentPageRoute()
  49. ],
  50. builder: (context, child, pageController) {
  51. final tabsRouter = AutoTabsRouter.of(context);
  52. tabsRouter.addListener((){
  53. int currentIndex = tabsRouter.activeIndex;
  54. Log.d("MyPostsPage onPageChanged: $currentIndex");
  55. // vm.handlerChangeTab(context, currentIndex);
  56. // vm.setCurrentTabIndex(currentIndex);
  57. });
  58. // 恢复页面索引
  59. // vm.restorePageViewIndex(pageController);
  60. return Column(
  61. children: [
  62. // tab 标签
  63. _buildHeaderTabs(context, ref, state),
  64. Expanded(
  65. child: child,
  66. )
  67. ],
  68. );
  69. },
  70. )
  71. )
  72. ],
  73. ),
  74. );
  75. }
  76. Widget _buildHeaderTabs(BuildContext context, WidgetRef ref, state) {
  77. final vm = ref.read(myPostsVmProvider.notifier);
  78. return Container(
  79. color: context.appColors.textWhite,
  80. child: MyPostsTab(
  81. key: UniqueKey(),
  82. tabsList: state.tabsList,
  83. tabsRouter: null,
  84. onClickAction:(activeTabIdx){
  85. Log.d("点击的tab index: $activeTabIdx");
  86. vm.handlerChangeTab(context, activeTabIdx);
  87. },
  88. ),
  89. );
  90. }
  91. }