notifications_screen.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:shimmer/shimmer.dart';
  5. import '../../../core/l10n/app_localizations.dart';
  6. import '../../../core/theme/app_colors.dart';
  7. import '../../../data/models/announcement/announcement.dart';
  8. import '../../../providers/announcement_unread_provider.dart';
  9. import '../../../providers/notifications_provider.dart';
  10. import '../../widgets/common/app_refresh_indicator.dart';
  11. class NotificationsScreen extends ConsumerStatefulWidget {
  12. const NotificationsScreen({super.key});
  13. @override
  14. ConsumerState<NotificationsScreen> createState() =>
  15. _NotificationsScreenState();
  16. }
  17. class _NotificationsScreenState extends ConsumerState<NotificationsScreen> {
  18. @override
  19. void initState() {
  20. super.initState();
  21. WidgetsBinding.instance.addPostFrameCallback((_) {
  22. ref.invalidate(announcementUnreadProvider);
  23. });
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. final cs = Theme.of(context).colorScheme;
  28. final state = ref.watch(notificationsProvider);
  29. final notifier = ref.read(notificationsProvider.notifier);
  30. final unreadState = ref.watch(announcementUnreadProvider).valueOrNull;
  31. return Scaffold(
  32. appBar: AppBar(
  33. elevation: 0,
  34. leading: IconButton(
  35. icon: const Icon(Icons.chevron_left, size: 28),
  36. onPressed: () => context.pop(),
  37. ),
  38. title: Text(
  39. AppLocalizations.of(context)!.systemAnnouncement,
  40. style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
  41. ),
  42. centerTitle: true,
  43. ),
  44. body: _buildBody(context, cs, state, notifier, unreadState),
  45. );
  46. }
  47. Widget _buildBody(
  48. BuildContext context,
  49. ColorScheme cs,
  50. AppNotificationsState state,
  51. AppNotificationsNotifier notifier,
  52. AnnouncementUnreadState? unreadState,
  53. ) {
  54. // 首次加载 → shimmer 骨架屏
  55. if (state.isLoading && state.notifications.isEmpty) {
  56. return _ShimmerList(cs: cs);
  57. }
  58. // 空状态
  59. if (state.notifications.isEmpty) {
  60. return Center(
  61. child: Text(
  62. AppLocalizations.of(context)!.noAnnouncement,
  63. style: TextStyle(color: cs.onSurface.withAlpha(153)),
  64. ),
  65. );
  66. }
  67. return AppRefreshIndicator(
  68. onRefresh: notifier.refresh,
  69. child: NotificationListener<ScrollNotification>(
  70. onNotification: (notification) {
  71. final metrics = notification.metrics;
  72. // 滚动到底部附近 → 加载更多
  73. if (notification is ScrollEndNotification &&
  74. metrics.pixels >= metrics.maxScrollExtent - 100) {
  75. notifier.loadMore();
  76. }
  77. // 内容不足以滚动(全部可见)→ 自动触发加载更多
  78. if (notification is ScrollMetricsNotification &&
  79. metrics.maxScrollExtent == 0 &&
  80. state.hasMore &&
  81. !state.isLoading) {
  82. WidgetsBinding.instance
  83. .addPostFrameCallback((_) => notifier.loadMore());
  84. }
  85. return false;
  86. },
  87. child: ListView.separated(
  88. padding: const EdgeInsets.fromLTRB(16, 16, 16, 32),
  89. itemCount: state.notifications.length + 1,
  90. separatorBuilder: (_, __) => const SizedBox(height: 1),
  91. itemBuilder: (_, index) {
  92. if (index >= state.notifications.length) {
  93. if (state.isLoading) {
  94. return const Padding(
  95. padding: EdgeInsets.symmetric(vertical: 16),
  96. child: Center(
  97. child: CircularProgressIndicator(strokeWidth: 2)),
  98. );
  99. }
  100. if (!state.hasMore) {
  101. return Padding(
  102. padding: const EdgeInsets.symmetric(vertical: 16),
  103. child: Center(
  104. child: Text(
  105. AppLocalizations.of(context)!.noMore,
  106. style: TextStyle(
  107. color: cs.onSurface.withAlpha(102), fontSize: 13),
  108. ),
  109. ),
  110. );
  111. }
  112. // hasMore && !isLoading → 下拉提示
  113. return Padding(
  114. padding: const EdgeInsets.symmetric(vertical: 12),
  115. child: Center(
  116. child: Row(
  117. mainAxisSize: MainAxisSize.min,
  118. children: [
  119. Icon(Icons.keyboard_arrow_down,
  120. size: 18, color: cs.onSurface.withAlpha(102)),
  121. const SizedBox(width: 4),
  122. Text(
  123. AppLocalizations.of(context)!.pullDownToLoadMore,
  124. style: TextStyle(
  125. color: cs.onSurface.withAlpha(102), fontSize: 13),
  126. ),
  127. ],
  128. ),
  129. ),
  130. );
  131. }
  132. final item = state.notifications[index];
  133. final isFirst = index == 0;
  134. final isLast =
  135. index == state.notifications.length - 1 && !state.hasMore;
  136. return _NotificationRow(
  137. item: item,
  138. isFirst: isFirst,
  139. isLast: isLast,
  140. isUnread: !(unreadState?.isRead(int.tryParse(item.id) ?? -1) ?? true),
  141. onTap: () => context.push('/user/messages/${item.id}'),
  142. );
  143. },
  144. ),
  145. ),
  146. );
  147. }
  148. }
  149. // ── Shimmer 骨架屏 ──────────────────────────────────────────
  150. class _ShimmerList extends StatelessWidget {
  151. const _ShimmerList({required this.cs});
  152. final ColorScheme cs;
  153. @override
  154. Widget build(BuildContext context) {
  155. return Shimmer.fromColors(
  156. baseColor: cs.onSurface.withAlpha(15),
  157. highlightColor: cs.onSurface.withAlpha(30),
  158. child: Padding(
  159. padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
  160. child: Container(
  161. decoration: BoxDecoration(
  162. color: Colors.white,
  163. borderRadius: BorderRadius.circular(12),
  164. ),
  165. child: Column(
  166. mainAxisSize: MainAxisSize.min,
  167. children: List.generate(8, (i) {
  168. return Padding(
  169. padding:
  170. const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
  171. child: Row(
  172. children: [
  173. const SizedBox(width: 16),
  174. Expanded(
  175. child: Column(
  176. crossAxisAlignment: CrossAxisAlignment.start,
  177. children: [
  178. Container(
  179. height: 14,
  180. width: double.infinity,
  181. decoration: BoxDecoration(
  182. color: Colors.white,
  183. borderRadius: BorderRadius.circular(4),
  184. ),
  185. ),
  186. const SizedBox(height: 8),
  187. Container(
  188. height: 12,
  189. width: 120,
  190. decoration: BoxDecoration(
  191. color: Colors.white,
  192. borderRadius: BorderRadius.circular(4),
  193. ),
  194. ),
  195. ],
  196. ),
  197. ),
  198. const SizedBox(width: 8),
  199. Container(
  200. height: 18,
  201. width: 18,
  202. decoration: BoxDecoration(
  203. color: Colors.white,
  204. borderRadius: BorderRadius.circular(4),
  205. ),
  206. ),
  207. ],
  208. ),
  209. );
  210. }),
  211. ),
  212. ),
  213. ),
  214. );
  215. }
  216. }
  217. // ── 数据行 ──────────────────────────────────────────────────
  218. class _NotificationRow extends StatelessWidget {
  219. const _NotificationRow({
  220. required this.item,
  221. required this.isFirst,
  222. required this.isLast,
  223. required this.isUnread,
  224. required this.onTap,
  225. });
  226. final AnnouncementContent item;
  227. final bool isFirst;
  228. final bool isLast;
  229. final bool isUnread;
  230. final VoidCallback onTap;
  231. @override
  232. Widget build(BuildContext context) {
  233. final cs = Theme.of(context).colorScheme;
  234. final isDark = Theme.of(context).brightness == Brightness.dark;
  235. return Container(
  236. decoration: BoxDecoration(
  237. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  238. borderRadius: BorderRadius.vertical(
  239. top: isFirst ? const Radius.circular(12) : Radius.zero,
  240. bottom: isLast ? const Radius.circular(12) : Radius.zero,
  241. ),
  242. ),
  243. child: Column(
  244. children: [
  245. InkWell(
  246. onTap: onTap,
  247. borderRadius: BorderRadius.vertical(
  248. top: isFirst ? const Radius.circular(12) : Radius.zero,
  249. bottom: isLast ? const Radius.circular(12) : Radius.zero,
  250. ),
  251. child: Padding(
  252. padding:
  253. const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
  254. child: Row(
  255. children: [
  256. SizedBox(
  257. width: 10,
  258. child: isUnread
  259. ? Container(
  260. width: 8,
  261. height: 8,
  262. decoration: const BoxDecoration(
  263. color: AppColors.fall,
  264. shape: BoxShape.circle,
  265. ),
  266. )
  267. : null,
  268. ),
  269. const SizedBox(width: 6),
  270. Expanded(
  271. child: Column(
  272. crossAxisAlignment: CrossAxisAlignment.start,
  273. children: [
  274. Text(
  275. item.title,
  276. style: TextStyle(
  277. color: cs.onSurface,
  278. fontSize: 14,
  279. fontWeight: FontWeight.w500,
  280. ),
  281. maxLines: 2,
  282. overflow: TextOverflow.ellipsis,
  283. ),
  284. const SizedBox(height: 3),
  285. Text(
  286. item.createTime,
  287. style: TextStyle(
  288. color: cs.onSurface.withAlpha(153),
  289. fontSize: 12,
  290. ),
  291. ),
  292. ],
  293. ),
  294. ),
  295. Icon(Icons.chevron_right,
  296. size: 18, color: cs.onSurface.withAlpha(153)),
  297. ],
  298. ),
  299. ),
  300. ),
  301. if (!isLast)
  302. Divider(
  303. height: 1,
  304. indent: 16,
  305. color: cs.outline,
  306. ),
  307. ],
  308. ),
  309. );
  310. }
  311. }