You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
class ProfileController extends GetxController {
static ProfileController get to => Get.find();
final _userName = ''.obs;
final _email = ''.obs;
final _avatar = ''.obs;
final _isEditing = false.obs;
final _storage = GetStorage();
String get userName => _userName.value;
String get email => _email.value;
String get avatar => _avatar.value;
bool get isEditing => _isEditing.value;
@override
void onInit() {
super.onInit();
loadUserData();
}
void loadUserData() {
try {
_userName.value = _storage.read('userName') ?? 'User Name';
_email.value = _storage.read('userEmail') ?? 'user@example.com';
_avatar.value = _storage.read('userAvatar') ?? '';
} catch (e) {
Get.snackbar('Error', 'Failed to load user data');
}
}
Future<void> updateUserData({
String? userName,
String? email,
String? avatar,
}) async {
try {
if (userName != null) {
await _storage.write('userName', userName);
_userName.value = userName;
}
if (email != null) {
await _storage.write('userEmail', email);
_email.value = email;
}
if (avatar != null) {
await _storage.write('userAvatar', avatar);
_avatar.value = avatar;
}
Get.snackbar('Success', 'Profile updated successfully');
} catch (e) {
Get.snackbar('Error', 'Failed to update profile');
}
}
void toggleEditing() {
_isEditing.value = !_isEditing.value;
}
}