Refactor account settings dialog to load user data based on auth state changes and improve error handling with snack bars

This commit is contained in:
Leon Bösche
2026-01-29 00:35:00 +01:00
parent db86c985f5
commit 03329c10ed

View File

@@ -41,7 +41,6 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
_currentPasswordController = TextEditingController();
_newPasswordController = TextEditingController();
_confirmPasswordController = TextEditingController();
_loadUserData();
}
@override
@@ -53,15 +52,6 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
super.dispose();
}
void _loadUserData() {
final authState = context.read<AuthBloc>().state;
if (authState is AuthAuthenticated) {
_currentUser = authState.user;
_displayNameController.text = _currentUser?.displayName ?? '';
_avatarUrl = _currentUser?.avatarUrl;
}
}
Future<void> _pickAndUploadAvatar() async {
try {
final result = await FilePicker.platform.pickFiles(
@@ -135,12 +125,16 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
// Update auth state
context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('Profile updated')));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Profile updated successfully')),
);
}
} catch (e) {
setState(() => _error = e.toString());
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Failed to update profile: $e')));
}
} finally {
setState(() => _isLoading = false);
}
@@ -148,7 +142,11 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
Future<void> _changePassword() async {
if (_newPasswordController.text != _confirmPasswordController.text) {
setState(() => _error = 'Passwords do not match');
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('Passwords do not match')));
}
return;
}
@@ -160,9 +158,9 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
newPassword: _newPasswordController.text,
);
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('Password changed')));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Password changed successfully')),
);
}
// Clear fields
@@ -170,7 +168,11 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
_newPasswordController.clear();
_confirmPasswordController.clear();
} catch (e) {
setState(() => _error = e.toString());
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to change password: $e')),
);
}
} finally {
setState(() => _isLoading = false);
}
@@ -329,79 +331,92 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
@override
Widget build(BuildContext context) {
return Dialog(
backgroundColor: AppTheme.primaryBackground,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
width: 500,
height: 700,
padding: const EdgeInsets.all(24),
child: Column(
children: [
// Header
Row(
return BlocBuilder<AuthBloc, AuthState>(
builder: (context, authState) {
// Update user data when auth state changes
if (authState is AuthAuthenticated && _currentUser != authState.user) {
_currentUser = authState.user;
_displayNameController.text = _currentUser?.displayName ?? '';
_avatarUrl = _currentUser?.avatarUrl;
}
return Dialog(
backgroundColor: AppTheme.primaryBackground,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: Container(
width: 500,
height: 700,
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
'Account Settings',
style: TextStyle(
color: AppTheme.primaryText,
fontSize: 24,
fontWeight: FontWeight.bold,
),
// Header
Row(
children: [
Text(
'Account Settings',
style: TextStyle(
color: AppTheme.primaryText,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const Spacer(),
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: Icon(Icons.close, color: AppTheme.secondaryText),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
],
),
const Spacer(),
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: Icon(Icons.close, color: AppTheme.secondaryText),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
const SizedBox(height: 16),
// Tabs
Row(
children: [
_buildTabButton('Profile', 0),
_buildTabButton('Security', 1),
_buildTabButton('Subscription', 2),
],
),
const SizedBox(height: 16),
// Content
Expanded(
child: _isLoading
? Center(
child: CircularProgressIndicator(
color: AppTheme.accentColor,
),
)
: _error != null
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_error!,
style: TextStyle(color: AppTheme.errorColor),
),
const SizedBox(height: 16),
ModernGlassButton(
onPressed: () {
setState(() => _error = null);
},
child: const Text('Retry'),
),
],
),
)
: _buildTabContent(),
),
],
),
const SizedBox(height: 16),
// Tabs
Row(
children: [
_buildTabButton('Profile', 0),
_buildTabButton('Security', 1),
_buildTabButton('Subscription', 2),
],
),
const SizedBox(height: 16),
// Content
Expanded(
child: _isLoading
? Center(
child: CircularProgressIndicator(
color: AppTheme.accentColor,
),
)
: _error != null
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_error!,
style: TextStyle(color: AppTheme.errorColor),
),
const SizedBox(height: 16),
ModernGlassButton(
onPressed: () {
setState(() => _error = null);
},
child: const Text('Retry'),
),
],
),
)
: _buildTabContent(),
),
],
),
),
),
);
},
);
}