HydroNutrify_Ari_e41221567/lib/ui/pages/profile.dart

218 lines
6.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_monitoring/core/constants/constants.dart';
import 'package:mobile_monitoring/logic/controllers/controllers.dart';
import 'package:mobile_monitoring/ui/pages/login.dart';
import 'package:mobile_monitoring/ui/shared/shared.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final _controller = ProfileController();
@override
void initState() {
super.initState();
_controller.loadProfile();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _updateProfile() async {
final result = await _controller.updateProfile();
if (!mounted) return;
if (result.success) {
SuccessSnackbar.show(context, result.message);
} else {
ErrorSnackbar.show(context, result.message, type: ErrorType.general);
}
_controller.loadProfile();
}
Future<void> _showChangePasswordDialog() async {
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) => ChangePasswordDialog(
onChangePassword: (oldPassword, newPassword) async {
final result = await _controller.changePassword(
oldPassword: oldPassword,
newPassword: newPassword,
);
if (!result.success) {
throw Exception(result.message);
}
},
),
);
}
Future<void> _handleLogout() async {
final confirm = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Konfirmasi Logout'),
content: const Text('Apakah Anda yakin ingin keluar?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text(AppStrings.cancel),
),
ElevatedButton(
onPressed: () => Navigator.pop(context, true),
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.error,
foregroundColor: AppColors.white,
),
child: const Text('Keluar'),
),
],
),
);
if (confirm == true && mounted) {
final result = await _controller.logout();
if (!mounted) return;
if (result.success) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => const LoginPage()),
(route) => false,
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(result.message), backgroundColor: AppColors.error),
);
}
}
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: _controller,
builder: (context, _) {
final profile = _controller.profile;
return Scaffold(
appBar: AppBar(
title: const Text(AppStrings.profile),
backgroundColor: AppColors.primary,
foregroundColor: AppColors.white,
elevation: 0,
),
body: SingleChildScrollView(
child: Column(
children: [
// Profile Header dengan Avatar Inisial
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(vertical: 40),
decoration: const BoxDecoration(color: AppColors.secondary),
child: Column(
children: [
// Avatar dengan Inisial
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
border: Border.all(color: Colors.white, width: 4),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.2),
blurRadius: 8,
spreadRadius: 2,
),
],
),
child: Center(
child: Text(
profile?.initials ?? 'U',
style: const TextStyle(
fontSize: 36,
fontWeight: FontWeight.bold,
color: AppColors.secondary,
),
),
),
),
const SizedBox(height: 16),
Text(
profile?.displayName ?? 'Pengguna',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
profile?.email ?? '',
style: TextStyle(
fontSize: 14,
color: Colors.white.withValues(alpha: 0.9),
),
),
],
),
),
// Form Section
Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Profile Form Component
ProfileForm(
nameController: _controller.nameController,
isLoading: _controller.isLoading,
userEmail: profile?.email,
onUpdate: _updateProfile,
),
const SizedBox(height: 24),
// Account Actions Component
AccountActionsCard(
onChangePassword: _showChangePasswordDialog,
onLogout: _handleLogout,
),
const SizedBox(height: 16),
// App Version
Center(
child: Text(
'${AppStrings.appName} v${AppStrings.appVersion}',
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 12,
),
),
),
],
),
),
],
),
),
);
},
);
}
}