MIF_E31230069/spk_mobile/lib/screens/profile_screen.dart

421 lines
14 KiB
Dart

import 'package:flutter/material.dart';
import '../services/auth_service.dart';
import '../models/user.dart';
import '../login.dart';
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
final _authService = AuthService();
User? _currentUser;
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadUser();
}
Future<void> _loadUser() async {
setState(() => _isLoading = true);
final user = await _authService.getCurrentUser();
setState(() {
_currentUser = user;
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: SafeArea(
child: CustomScrollView(
slivers: [
// Header with Profile
SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF1565C0), Color(0xFF1976D2)],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
children: [
// Avatar
Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.white, width: 3),
),
child: CircleAvatar(
radius: 50,
backgroundColor: Colors.white,
child: Text(
_currentUser?.name.substring(0, 1).toUpperCase() ?? 'U',
style: const TextStyle(
fontSize: 36,
fontWeight: FontWeight.bold,
color: Color(0xFF1565C0),
),
),
),
),
const SizedBox(height: 16),
Text(
_currentUser?.name ?? 'User',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
_currentUser?.email ?? '',
style: TextStyle(
fontSize: 16,
color: Colors.white.withValues(alpha: 0.9),
),
),
],
),
),
),
// Menu Items
SliverPadding(
padding: const EdgeInsets.all(16),
sliver: SliverList(
delegate: SliverChildListDelegate([
_buildSection('Akun'),
_buildMenuItem(
icon: Icons.person,
title: 'Edit Profil',
subtitle: 'Ubah informasi profil Anda',
onTap: () {
// TODO: Navigate to edit profile
},
),
_buildMenuItem(
icon: Icons.lock,
title: 'Ubah Password',
subtitle: 'Ganti password akun Anda',
onTap: () {
// TODO: Navigate to change password
},
),
const SizedBox(height: 16),
_buildSection('Aktivitas'),
_buildMenuItem(
icon: Icons.bookmark,
title: 'Booking Saya',
subtitle: 'Lihat riwayat booking Anda',
onTap: () {
// Already on tabs, maybe switch tab?
},
),
_buildMenuItem(
icon: Icons.favorite,
title: 'Favorit',
subtitle: 'Kontrakan yang Anda favoritkan',
onTap: () {
// TODO: Navigate to favorites
},
),
_buildMenuItem(
icon: Icons.rate_review,
title: 'Review Saya',
subtitle: 'Lihat semua review Anda',
onTap: () {
// TODO: Navigate to my reviews
},
),
const SizedBox(height: 16),
_buildSection('Pengaturan'),
_buildMenuItem(
icon: Icons.notifications,
title: 'Notifikasi',
subtitle: 'Atur preferensi notifikasi',
onTap: () {
// TODO: Navigate to notifications settings
},
),
_buildMenuItem(
icon: Icons.language,
title: 'Bahasa',
subtitle: 'Pilih bahasa aplikasi',
trailing: const Text('Indonesia', style: TextStyle(color: Colors.grey)),
),
const SizedBox(height: 16),
_buildSection('Bantuan'),
_buildMenuItem(
icon: Icons.help,
title: 'Pusat Bantuan',
subtitle: 'FAQ dan panduan penggunaan',
onTap: () {
// TODO: Navigate to help center
},
),
_buildMenuItem(
icon: Icons.info,
title: 'Tentang Aplikasi',
subtitle: 'Versi 1.0.0',
onTap: () {
_showAboutDialog();
},
),
const SizedBox(height: 24),
// Logout Button
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _handleLogout,
icon: const Icon(Icons.logout),
label: const Text('Logout'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
const SizedBox(height: 24),
]),
),
),
],
),
),
);
}
Widget _buildSection(String title) {
return Padding(
padding: const EdgeInsets.only(left: 4, bottom: 8, top: 8),
child: Text(
title,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.grey[700],
letterSpacing: 0.5,
),
),
);
}
Widget _buildMenuItem({
required IconData icon,
required String title,
required String subtitle,
VoidCallback? onTap,
Widget? trailing,
}) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: ListTile(
onTap: onTap,
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xFF1565C0).withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: const Color(0xFF1565C0), size: 24),
),
title: Text(
title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.black87,
),
),
subtitle: Text(
subtitle,
style: TextStyle(
fontSize: 13,
color: Colors.grey[600],
),
),
trailing: trailing ??
(onTap != null
? const Icon(Icons.chevron_right, color: Colors.grey)
: null),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
),
);
}
void _showAboutDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
title: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xFF1565C0).withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: const Icon(
Icons.home_work,
color: Color(0xFF1565C0),
size: 32,
),
),
const SizedBox(width: 12),
const Text('SPK Kontrakan'),
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Sistem Pendukung Keputusan',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
Text(
'Aplikasi untuk membantu mahasiswa menemukan kontrakan terbaik dengan metode SAW (Simple Additive Weighting)',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
const SizedBox(height: 16),
const Divider(),
const SizedBox(height: 8),
_buildInfoRow('Versi', '1.0.0'),
_buildInfoRow('Developer', 'SPK Team'),
_buildInfoRow('Tahun', '2026'),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Tutup'),
),
],
),
);
}
Widget _buildInfoRow(String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
label,
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
Text(
value,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Colors.black87,
),
),
],
),
);
}
Future<void> _handleLogout() async {
final confirm = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
title: Row(
children: [
Icon(Icons.logout, color: Colors.red[700]),
const SizedBox(width: 12),
const Text('Konfirmasi Logout'),
],
),
content: const Text('Apakah Anda yakin ingin keluar dari aplikasi?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Batal'),
),
ElevatedButton(
onPressed: () => Navigator.pop(context, true),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text('Logout'),
),
],
),
);
if (confirm == true) {
await _authService.logout();
if (mounted) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
(route) => false,
);
}
}
}
}