feat: Implement change detection and conditional enabling of save/cancel buttons for profile and notification settings.

This commit is contained in:
pradeepkumar
2026-03-20 13:29:28 +05:30
parent f3e48925b2
commit ea7c003e6a
3 changed files with 80 additions and 27 deletions

View File

@@ -28,6 +28,7 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
bool _controllersInitialized = false;
bool _isUploadingAvatar = false;
File? _localAvatarFile;
Map<String, String> _savedValues = {};
@override
void initState() {
@@ -81,6 +82,21 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
final parts = [city, stateName, country].where((s) => s.isNotEmpty);
_locationController.text = parts.join(', ');
}
_savedValues = {
'fullName': _fullNameController.text,
'title': _titleController.text,
'email': _emailController.text,
'phone': _phoneController.text,
'location': _locationController.text,
};
}
bool get _hasChanges {
return _fullNameController.text != (_savedValues['fullName'] ?? '') ||
_titleController.text != (_savedValues['title'] ?? '') ||
_phoneController.text != (_savedValues['phone'] ?? '') ||
_locationController.text != (_savedValues['location'] ?? '');
}
@override
@@ -262,6 +278,7 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
onSave: _saveProfileSettings,
onCancel: _resetForm,
isSaving: profileState.isSaving,
hasChanges: _hasChanges,
),
],
);
@@ -338,16 +355,16 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
}
void _resetForm() {
_controllersInitialized = false;
_localAvatarFile = null;
final profileState = ref.read(profileProvider);
if (profileState.profile.isNotEmpty) {
_initControllersFromProfile(profileState);
}
if (!_hasChanges) return;
// Revert to last saved values
_fullNameController.text = _savedValues['fullName'] ?? '';
_titleController.text = _savedValues['title'] ?? '';
_phoneController.text = _savedValues['phone'] ?? '';
_locationController.text = _savedValues['location'] ?? '';
setState(() {});
}
void _saveProfileSettings() {
Future<void> _saveProfileSettings() async {
final profileState = ref.read(profileProvider);
final isAgent = profileState.isAgent;
@@ -366,7 +383,6 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
final location = _locationController.text.trim();
if (location.isNotEmpty) data['serviceAreas'] = [location];
} else {
// Note: headline is not a field on UserProfile — don't send it
final locationParts = _locationController.text
.split(',')
.map((s) => s.trim())
@@ -377,7 +393,20 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
if (locationParts.length > 2) data['country'] = locationParts[2];
}
ref.read(profileProvider.notifier).updateProfile(data);
await ref.read(profileProvider.notifier).updateProfile(data);
// Update saved values so Cancel won't revert after successful save
if (mounted) {
setState(() {
_savedValues = {
'fullName': _fullNameController.text,
'title': _titleController.text,
'email': _emailController.text,
'phone': _phoneController.text,
'location': _locationController.text,
};
});
}
}
void _showSnackBar(String message, {bool isError = false}) {