my_posts_page.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // // 创建 GlobalKey
  31. // final GlobalKey<AutoTabsRouterState> _tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  32. return Scaffold(
  33. appBar: MyAppBar.appBar(
  34. context,
  35. "My Posts",
  36. backgroundColor: context.appColors.whiteBG,
  37. ),
  38. backgroundColor: context.appColors.backgroundDefault,
  39. body: Column(
  40. children: [
  41. Expanded(
  42. child: AutoTabsRouter.pageView(
  43. key: UniqueKey(),
  44. routes: const [
  45. MyPostsNewsfeedPageRoute(),
  46. MyPostsForSalePageRoute(),
  47. MyPostsForRentPageRoute()
  48. ],
  49. builder: (context, child, animation) {
  50. final tabsRouter = AutoTabsRouter.of(context);
  51. return Column(
  52. children: [
  53. _buildHeaderTabs(context, ref, state),
  54. Expanded(
  55. child: child,
  56. )
  57. ],
  58. );
  59. },
  60. )
  61. )
  62. ],
  63. ),
  64. );
  65. }
  66. Widget _buildHeaderTabs(BuildContext context, WidgetRef ref, state) {
  67. final vm = ref.read(myPostsVmProvider.notifier);
  68. return Container(
  69. color: Colors.blue,
  70. child: MyPostsTab(
  71. key: UniqueKey(),
  72. tabsList: state.tabsList,
  73. tabsRouter: null,
  74. onClickAction:(activeTabIdx){
  75. Log.d("点击的tab index: $activeTabIdx");
  76. vm.handlerChangeTab(context, activeTabIdx);
  77. },
  78. ),
  79. );
  80. }
  81. }