message_list_page.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import 'package:easy_refresh/easy_refresh.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:tdesign_flutter/tdesign_flutter.dart';
  6. import '../../shared/widgets/empty_state.dart';
  7. import '../shell/nav_bar_config.dart';
  8. import '../../core/i18n/app_localizations.dart';
  9. import '../../shared/widgets/message_item.dart';
  10. import '../../shared/widgets/skeleton_list_card.dart';
  11. import 'message_controller.dart';
  12. import 'message_model.dart';
  13. import '../../core/theme/app_colors.dart';
  14. import '../../core/theme/app_colors_extension.dart';
  15. /// 消息通知聚合页
  16. ///
  17. /// 展示 5 种消息类型:
  18. /// - 审批待办(📋)→ 详情页 + 审批操作栏
  19. /// - 审批结果(✅/❌)→ 详情查看结果
  20. /// - 撤回通知(↩)→ 消息列表
  21. /// - 系统公告(📢)→ 公告详情
  22. /// - 过期提醒(⏰)→ 对应详情页
  23. ///
  24. /// 左滑操作:标记已读(蓝)+ 删除(红)。已读消息不可左滑。
  25. class MessageListPage extends ConsumerWidget {
  26. const MessageListPage({super.key});
  27. @override
  28. Widget build(BuildContext context, WidgetRef ref) {
  29. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  30. final messagesAsync = ref.watch(messageListProvider);
  31. final unreadCount = ref.watch(unreadCountProvider);
  32. final location = GoRouterState.of(context).uri.toString();
  33. final l10n = AppLocalizations.of(context);
  34. if (location.startsWith('/messages')) {
  35. ref
  36. .read(navBarConfigProvider.notifier)
  37. .update(
  38. NavBarConfig(
  39. title: l10n.get('messageNotifications'),
  40. showBack: true,
  41. leadingIcon: Icons.close,
  42. // 仅在有未读时显示"全部已读"按钮
  43. showRight: unreadCount > 0,
  44. rightWidget: unreadCount > 0
  45. ? GestureDetector(
  46. onTap: () {
  47. TDMessage.showMessage(
  48. context: context,
  49. content: l10n.get('markAllRead'),
  50. theme: MessageTheme.success,
  51. icon: true,
  52. duration: 2000,
  53. );
  54. },
  55. child: Padding(
  56. padding: const EdgeInsets.symmetric(
  57. horizontal: 12,
  58. vertical: 8,
  59. ),
  60. child: Text(
  61. l10n.get('markAllRead'),
  62. style: TextStyle(
  63. fontSize: AppFontSizes.body,
  64. color: colors.primary,
  65. ),
  66. ),
  67. ),
  68. )
  69. : null,
  70. ),
  71. );
  72. }
  73. // 首次加载:展示骨架屏,不包裹 EasyRefresh
  74. if (messagesAsync.isLoading && !messagesAsync.hasValue) {
  75. return SkeletonLoadingList(
  76. cardBuilder: () => const SkeletonMessageCard(),
  77. );
  78. }
  79. return EasyRefresh(
  80. header: TDRefreshHeader(),
  81. onRefresh: () async {
  82. ref.invalidate(messageListProvider);
  83. await ref.read(messageListProvider.future);
  84. },
  85. child: _buildContent(context, ref, messagesAsync, l10n),
  86. );
  87. }
  88. /// 根据加载状态构建列表内容
  89. ///
  90. /// - [AsyncValue.isReloading]:下拉刷新中,展示旧数据 + header loading
  91. /// - [AsyncValue.hasError]:错误状态
  92. /// - 空数据:空状态
  93. /// - 正常数据:消息列表
  94. Widget _buildContent(
  95. BuildContext context,
  96. WidgetRef ref,
  97. AsyncValue<List<MessageModel>> messagesAsync,
  98. AppLocalizations l10n,
  99. ) {
  100. // 下拉刷新中:保留旧数据 + EasyRefresh header loading
  101. if (messagesAsync.isReloading) {
  102. final oldItems = messagesAsync.valueOrNull ?? [];
  103. if (oldItems.isEmpty) {
  104. return SingleChildScrollView(
  105. physics: const AlwaysScrollableScrollPhysics(),
  106. child: EmptyState(message: l10n.get('noMessages')),
  107. );
  108. }
  109. return ListView.separated(
  110. padding: const EdgeInsets.fromLTRB(
  111. AppSpacing.md,
  112. AppSpacing.md,
  113. AppSpacing.md,
  114. AppSpacing.lg,
  115. ),
  116. itemCount: oldItems.length,
  117. separatorBuilder: (_, _) => const SizedBox(height: 12),
  118. itemBuilder: (_, i) => _buildItem(context, ref, oldItems[i], l10n),
  119. );
  120. }
  121. // 错误
  122. if (messagesAsync.hasError) {
  123. return SingleChildScrollView(
  124. physics: const AlwaysScrollableScrollPhysics(),
  125. child: EmptyState(message: l10n.get('loadFailed')),
  126. );
  127. }
  128. // 空数据
  129. final messages = messagesAsync.requireValue;
  130. if (messages.isEmpty) {
  131. return SingleChildScrollView(
  132. physics: const AlwaysScrollableScrollPhysics(),
  133. child: EmptyState(message: l10n.get('noMessages')),
  134. );
  135. }
  136. // 正常列表
  137. return ListView.separated(
  138. padding: const EdgeInsets.fromLTRB(
  139. AppSpacing.md,
  140. AppSpacing.md,
  141. AppSpacing.md,
  142. AppSpacing.lg,
  143. ),
  144. itemCount: messages.length,
  145. separatorBuilder: (_, _) => const SizedBox(height: 12),
  146. itemBuilder: (_, i) => _buildItem(context, ref, messages[i], l10n),
  147. );
  148. }
  149. Widget _buildItem(
  150. BuildContext context,
  151. WidgetRef ref,
  152. MessageModel msg,
  153. AppLocalizations l10n,
  154. ) {
  155. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  156. final (icon, iconColor, iconBg) = _messageIconProps(msg.msgType, colors);
  157. return MessageItem(
  158. icon: icon,
  159. iconColor: iconColor,
  160. iconBackground: iconBg,
  161. title: msg.title,
  162. time: _formatTime(msg.createTime),
  163. sender: _messageSender(msg.msgType, l10n),
  164. summary: msg.content,
  165. unread: !msg.isRead,
  166. onTap: () => _navigateToBiz(context, msg),
  167. // 仅未读消息展示左滑操作
  168. onToggleRead: msg.isRead
  169. ? null
  170. : () {
  171. TDMessage.showMessage(
  172. context: context,
  173. content: '${l10n.get("markRead")}:${msg.title}',
  174. theme: MessageTheme.success,
  175. icon: true,
  176. duration: 2000,
  177. );
  178. },
  179. onDelete: () {
  180. showDialog(
  181. context: context,
  182. builder: (ctx) => TDAlertDialog(
  183. title: l10n.get('confirm'),
  184. content: l10n.getString(
  185. 'confirmAction',
  186. args: {'action': l10n.get('delete')},
  187. ),
  188. leftBtn: TDDialogButtonOptions(
  189. title: l10n.get('cancel'),
  190. action: () => Navigator.of(ctx).pop(),
  191. ),
  192. rightBtn: TDDialogButtonOptions(
  193. title: l10n.get('confirm'),
  194. action: () {
  195. Navigator.of(ctx).pop();
  196. TDMessage.showMessage(
  197. context: context,
  198. content: '${l10n.get("deletedToast")}${msg.title}',
  199. theme: MessageTheme.warning,
  200. icon: true,
  201. duration: 2000,
  202. );
  203. },
  204. ),
  205. ),
  206. );
  207. },
  208. );
  209. }
  210. /// 按消息类型返回(图标, 图标色, 图标背景色)
  211. (IconData, Color, Color) _messageIconProps(
  212. String msgType,
  213. AppColorsExtension colors,
  214. ) {
  215. switch (msgType) {
  216. case 'announcement':
  217. return (Icons.campaign_outlined, colors.warning, colors.warningBg);
  218. case 'approval_result':
  219. return (Icons.check_circle_outlined, colors.success, colors.successBg);
  220. case 'withdraw_notice':
  221. return (Icons.undo_outlined, colors.statusGray, colors.swipeDeleteBg);
  222. case 'expiry_reminder':
  223. return (Icons.timer_off_outlined, colors.danger, colors.dangerBg);
  224. case 'approval_notice':
  225. default:
  226. return (Icons.assignment_outlined, colors.primary, colors.primaryLight);
  227. }
  228. }
  229. /// 按消息类型返回发送者标签
  230. String _messageSender(String msgType, AppLocalizations l10n) {
  231. switch (msgType) {
  232. case 'announcement':
  233. return l10n.get('systemNotice');
  234. case 'approval_notice':
  235. return l10n.get('approvalNotice');
  236. case 'approval_result':
  237. return l10n.get('systemMessage');
  238. case 'withdraw_notice':
  239. return l10n.get('systemMessage');
  240. case 'expiry_reminder':
  241. return l10n.get('systemMessage');
  242. default:
  243. return l10n.get('systemMessage');
  244. }
  245. }
  246. /// 格式化时间为 MM-DD HH:mm
  247. String _formatTime(DateTime time) {
  248. final month = time.month.toString().padLeft(2, '0');
  249. final day = time.day.toString().padLeft(2, '0');
  250. final hour = time.hour.toString().padLeft(2, '0');
  251. final minute = time.minute.toString().padLeft(2, '0');
  252. return '$month-$day $hour:$minute';
  253. }
  254. /// 按 BizType 路由跳转
  255. void _navigateToBiz(BuildContext context, MessageModel msg) {
  256. if (msg.bizType == null || msg.bizId == null) return;
  257. switch (msg.bizType) {
  258. case 'expense':
  259. context.push('/expense/detail/${msg.bizId}');
  260. break;
  261. case 'overtime':
  262. context.push('/overtime/detail/${msg.bizId}');
  263. break;
  264. case 'vehicle':
  265. context.push('/vehicle/detail/${msg.bizId}');
  266. break;
  267. case 'announcement':
  268. context.push('/announcement/detail/${msg.bizId}');
  269. break;
  270. case 'expense_application':
  271. context.push('/expense-apply/detail/${msg.bizId}');
  272. break;
  273. }
  274. }
  275. }