notification_page.dart 3.2 KB

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