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,9 +331,20 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
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(
|
return Dialog(
|
||||||
backgroundColor: AppTheme.primaryBackground,
|
backgroundColor: AppTheme.primaryBackground,
|
||||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(16),
|
||||||
|
),
|
||||||
child: Container(
|
child: Container(
|
||||||
width: 500,
|
width: 500,
|
||||||
height: 700,
|
height: 700,
|
||||||
@@ -403,6 +416,8 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildTabButton(String text, int index) {
|
Widget _buildTabButton(String text, int index) {
|
||||||
|
|||||||
Reference in New Issue
Block a user