Files
LearnIT/lib/features/settings/presentation/pages/profile_edit_page.dart
2026-05-24 17:39:10 +01:00

305 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../../core/services/auth_service.dart';
import '../../../../core/theme/app_colors.dart';
import '../../../dashboard/presentation/pages/student_dashboard_page.dart';
import '../../../dashboard/presentation/pages/teacher_dashboard_page.dart';
/// Profile edit page for settings
class ProfileEditPage extends ConsumerStatefulWidget {
const ProfileEditPage({super.key});
@override
ConsumerState<ProfileEditPage> createState() => _ProfileEditPageState();
}
class _ProfileEditPageState extends ConsumerState<ProfileEditPage> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
bool _isLoading = false;
@override
void initState() {
super.initState();
_loadUserData();
}
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
Future<void> _loadUserData() async {
final user = AuthService.currentUser;
if (user != null) {
setState(() {
_nameController.text = user.displayName ?? '';
});
}
}
Future<void> _saveProfile() async {
if (!_formKey.currentState!.validate()) {
return;
}
setState(() {
_isLoading = true;
});
try {
final user = AuthService.currentUser;
if (user != null) {
await user.updateDisplayName(_nameController.text);
await user.reload();
// Clear cached user name so dashboard will reload with new name
StudentDashboardPage.clearCachedUserName();
TeacherDashboardPage.clearCachedUserName();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Perfil atualizado com sucesso!'),
backgroundColor: AppColors.primaryTeal,
),
);
context.pop();
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Erro ao atualizar perfil: $e'),
backgroundColor: AppColors.error,
),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
final user = AuthService.currentUser;
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) {
context.pop();
}
},
child: Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Theme.of(context).colorScheme.background,
Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
Theme.of(context).colorScheme.secondary.withValues(alpha: 0.05),
Theme.of(context).colorScheme.background,
],
),
),
child: SafeArea(
top: false,
child: Column(
children: [
// Custom AppBar
Padding(
padding: const EdgeInsets.only(
left: 16.0,
right: 16.0,
bottom: 20.0,
top: 52.0,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () => context.pop(),
),
const Expanded(
child: Text(
'Editar Perfil',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
// Form content
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Theme.of(
context,
).colorScheme.shadow.withValues(alpha: 0.1),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Avatar section
Center(
child: Column(
children: [
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [
Color(0xFF82C9BD),
Color(0xFF6BA8A0),
],
),
borderRadius: BorderRadius.circular(40),
),
child: const Icon(
Icons.person,
color: Colors.white,
size: 40,
),
),
const SizedBox(height: 16),
Text(
user?.displayName ?? 'Utilizador',
style: TextStyle(
color: Theme.of(
context,
).colorScheme.onSurface,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
user?.email ?? '',
style: TextStyle(
color: Theme.of(
context,
).colorScheme.onSurfaceVariant,
fontSize: 14,
),
),
],
),
),
const SizedBox(height: 32),
// Name field
Text(
'Nome',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurface,
),
),
const SizedBox(height: 8),
TextFormField(
controller: _nameController,
decoration: InputDecoration(
hintText: 'Introduza o seu nome',
filled: true,
fillColor: Theme.of(
context,
).colorScheme.surface,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor introduza o seu nome';
}
return null;
},
),
const SizedBox(height: 32),
// Save button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _saveProfile,
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.primaryTeal,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
vertical: 16,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor:
AlwaysStoppedAnimation<Color>(
Colors.white,
),
),
)
: const Text(
'Guardar',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
),
),
),
),
],
),
),
),
),
);
}
}