From 0030e025b81eafb07d07f65576f703d9105e9780 Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Tue, 3 Jun 2025 13:24:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=9A=E7=9F=A5=E6=8C=89?= =?UTF-8?q?=E9=88=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/home_content_page.dart | 75 +++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/lib/home_content_page.dart b/lib/home_content_page.dart index 1c686d8..b20e18f 100644 --- a/lib/home_content_page.dart +++ b/lib/home_content_page.dart @@ -7,6 +7,76 @@ import 'reapair.dart'; import 'activity.dart'; import 'announcement.dart'; +class NotificationIcon extends StatefulWidget { + const NotificationIcon({super.key}); + + @override + State createState() => _NotificationIconState(); +} + +class _NotificationIconState extends State { + List _notifications = []; + int get _unreadCount => _notifications.length; + + @override + void initState() { + super.initState(); + _fetchNotifications(); // 模擬 API 呼叫 + } + + void _fetchNotifications() async { + // 模擬從 API 取得資料 + // await Future.delayed(const Duration(seconds: 1)); + setState(() { + _notifications = ['4/15 管理費已出帳', '4/14 社區電梯保養通知', '4/13 新活動報名開始']; + }); + } + + @override + Widget build(BuildContext context) { + return Stack( + clipBehavior: Clip.none, + children: [ + PopupMenuButton( + icon: const Icon(Icons.notifications), + offset: const Offset(0, 40), // 👈 向下移開 40px,避免蓋住鈴鐺 + onSelected: (value) { + ScaffoldMessenger.of( + context, + ).showSnackBar(SnackBar(content: Text('點擊通知:$value'))); + }, + itemBuilder: (BuildContext context) { + return _notifications.map((String notification) { + return PopupMenuItem( + value: notification, + child: Text(notification), + ); + }).toList(); + }, + ), + if (_unreadCount > 0) + Positioned( + right: 4, + top: 4, + child: Container( + padding: const EdgeInsets.all(2), + decoration: BoxDecoration( + color: Colors.red, + borderRadius: BorderRadius.circular(10), + ), + constraints: const BoxConstraints(minWidth: 16, minHeight: 16), + child: Text( + '$_unreadCount', + style: const TextStyle(color: Colors.white, fontSize: 10), + textAlign: TextAlign.center, + ), + ), + ), + ], + ); + } +} + class HomeContentPage extends StatelessWidget { const HomeContentPage({super.key}); @@ -23,7 +93,8 @@ class HomeContentPage extends StatelessWidget { style: TextStyle(fontWeight: FontWeight.bold), ), const SizedBox(width: 8), - Stack( + NotificationIcon(), + /*Stack( children: [ IconButton( icon: const Icon(Icons.notifications), @@ -52,7 +123,7 @@ class HomeContentPage extends StatelessWidget { ), ), ], - ), + ),*/ ], ), ],