message_list_page.dart 8.1 KB

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