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