notification_page.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'package:cpt_main/modules/notification/item_notification_header.dart';
  2. import 'package:cpt_main/router/page/main_page_router.dart';
  3. import 'package:cs_resources/generated/l10n.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:auto_route/auto_route.dart';
  7. import 'package:flutter_hooks/flutter_hooks.dart';
  8. import 'package:hooks_riverpod/hooks_riverpod.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:widgets/load_state_layout.dart';
  11. import 'package:widgets/my_appbar.dart';
  12. import 'package:widgets/my_text_view.dart';
  13. import 'package:widgets/widget_export.dart';
  14. import 'item_notification.dart';
  15. import 'notification_view_model.dart';
  16. @RoutePage()
  17. class NotificationPage extends HookConsumerWidget {
  18. const NotificationPage({Key? key}) : super(key: key);
  19. static void startInstance({BuildContext? context}) {
  20. if (context != null) {
  21. context.router.push(const NotificationPageRoute());
  22. } else {
  23. appRouter.push(const NotificationPageRoute());
  24. }
  25. }
  26. @override
  27. Widget build(BuildContext context, WidgetRef ref) {
  28. final viewModel = ref.read(notificationViewModelProvider.notifier);
  29. final state = ref.watch(notificationViewModelProvider);
  30. useEffect(() {
  31. // 组件挂载时执行 - 执行接口请求
  32. Future.microtask(() => viewModel.fetchList());
  33. return () {
  34. // 组件卸载时执行
  35. };
  36. }, []);
  37. return Scaffold(
  38. appBar: MyAppBar.appBar(context, S.current.notification,
  39. backgroundColor: context.appColors.whiteBG,
  40. actions: [
  41. Center(
  42. child: MyTextView(
  43. S.current.mark_all,
  44. fontSize: 16,
  45. textColor: context.appColors.textPrimary,
  46. onClick: viewModel.markAll,
  47. isFontRegular: true,
  48. marginRight: 15,
  49. )),
  50. ],
  51. showBottomDivider: false),
  52. backgroundColor: context.appColors.whiteBG,
  53. body: SizedBox(
  54. width: double.infinity,
  55. height: double.infinity,
  56. child: EasyRefresh(
  57. controller: viewModel.refreshController,
  58. onRefresh: viewModel.onRefresh,
  59. onLoad: viewModel.loadMore,
  60. child: LoadStateLayout(
  61. state: state.loadingState,
  62. errorMessage: state.errorMessage,
  63. errorRetry: () {
  64. viewModel.retryRequest();
  65. },
  66. successSliverWidget: [
  67. SliverList(
  68. delegate: SliverChildBuilderDelegate(
  69. (context, index) {
  70. return StickyHeader(
  71. header: NotificationItemHeader(state.datas[index].groupId),
  72. content: Column(
  73. children: _buildGroup(state.datas[index].groupDatas!, index),
  74. ),
  75. );
  76. },
  77. childCount: state.datas.length,
  78. ))
  79. ],
  80. ),
  81. ),
  82. ),
  83. );
  84. }
  85. //当前组内的子数据
  86. _buildGroup(List<String> list, int index) {
  87. return list.map((item) {
  88. return NotificationItem(item: item, index: index);
  89. }).toList();
  90. }
  91. }