MIF_E31231033/lib/screens/dashboard/user/notification_screen.dart

238 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../services/notification_service.dart';
class NotificationPage extends StatefulWidget {
const NotificationPage({super.key});
@override
State<NotificationPage> createState() => _NotificationPageState();
}
class _NotificationPageState extends State<NotificationPage> {
List<NotificationItem> _notifications = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadNotifications();
}
Future<void> _loadNotifications() async {
final list = await NotificationService.getLocalNotifications();
if (mounted) {
setState(() {
_notifications = list;
_isLoading = false;
});
}
}
Future<void> _markAllRead() async {
await NotificationService.markAllAsRead();
await _loadNotifications();
}
Future<void> _markRead(String id) async {
await NotificationService.markAsRead(id);
await _loadNotifications();
}
String _formatWaktu(String iso) {
try {
final dt = DateTime.parse(iso).toLocal();
return DateFormat('dd MMM yyyy, HH:mm', 'id_ID').format(dt);
} catch (_) {
return iso;
}
}
@override
Widget build(BuildContext context) {
final unread = _notifications.where((n) => !n.isRead).length;
return Scaffold(
backgroundColor: const Color(0xFFF8F9FD),
appBar: AppBar(
backgroundColor: const Color(0xFF1A39B1),
foregroundColor: Colors.white,
elevation: 0,
title: const Text(
'Notifikasi',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
actions: [
if (unread > 0)
TextButton(
onPressed: _markAllRead,
child: const Text(
'Tandai Semua',
style: TextStyle(color: Colors.white70, fontSize: 12),
),
),
],
),
body:
_isLoading
? const Center(child: CircularProgressIndicator())
: _notifications.isEmpty
? _buildEmptyState()
: ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: _notifications.length,
separatorBuilder: (_, __) => const SizedBox(height: 10),
itemBuilder: (context, index) {
final notif = _notifications[index];
return _buildNotifTile(notif);
},
),
);
}
Widget _buildNotifTile(NotificationItem notif) {
final isJadwal = notif.type == 'jadwal';
final color = isJadwal ? const Color(0xFF3B82F6) : const Color(0xFFEF4444);
final bgColor =
isJadwal ? const Color(0xFFEFF6FF) : const Color(0xFFFEF2F2);
final icon = isJadwal ? Icons.calendar_month_rounded : Icons.cancel_rounded;
return GestureDetector(
onTap: () => _markRead(notif.id),
child: AnimatedContainer(
duration: const Duration(milliseconds: 300),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: notif.isRead ? Colors.white : bgColor,
borderRadius: BorderRadius.circular(18),
border: Border.all(
color: notif.isRead ? Colors.grey.shade100 : color.withOpacity(0.3),
width: 1.2,
),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.03),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Icon
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: color.withOpacity(0.12),
borderRadius: BorderRadius.circular(14),
),
child: Icon(icon, color: color, size: 22),
),
const SizedBox(width: 14),
// Konten
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
notif.title,
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 14,
color:
notif.isRead
? const Color(0xFF64748B)
: const Color(0xFF1E293B),
),
),
),
if (!notif.isRead)
Container(
width: 8,
height: 8,
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
),
),
],
),
const SizedBox(height: 5),
Text(
notif.message,
style: TextStyle(
fontSize: 13,
color: Colors.grey.shade600,
height: 1.5,
),
),
const SizedBox(height: 8),
Row(
children: [
Icon(
Icons.access_time_rounded,
size: 12,
color: Colors.grey.shade400,
),
const SizedBox(width: 4),
Text(
_formatWaktu(notif.waktu),
style: TextStyle(
fontSize: 11,
color: Colors.grey.shade400,
),
),
],
),
],
),
),
],
),
),
);
}
Widget _buildEmptyState() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: const Color(0xFFF1F5F9),
shape: BoxShape.circle,
),
child: const Icon(
Icons.notifications_none_rounded,
size: 48,
color: Color(0xFF94A3B8),
),
),
const SizedBox(height: 16),
const Text(
'Belum Ada Notifikasi',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: Color(0xFF475569),
),
),
const SizedBox(height: 6),
Text(
'Notifikasi jadwal dan laporan\nakan muncul di sini.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13, color: Colors.grey.shade400),
),
],
),
);
}
}