Browse Source

feat(profile): 添加退出时强制回滚未保存更改的功能

在用户退出编辑页面时自动检查并回滚未保存的更改,防止数据丢失。修改了编辑资料和会议资料视图,添加了WillPopScope处理返回操作。
weicu
lxm 1 year ago
parent
commit
2e59292dca
  1. 9
      lib/modules/profile/controllers/profile_controller.dart
  2. 68
      lib/modules/profile/views/profile_meeting_view.dart
  3. 31
      lib/modules/profile/views/profile_view.dart

9
lib/modules/profile/controllers/profile_controller.dart

@ -192,6 +192,15 @@ class ProfileController extends GetxController {
// }
// }
// 强制回滚未保存的更改(在页面退出时调用)
void forceRollbackIfUnsaved() {
if (hasEdits.value) {
Logger().d('检测到未保存的编辑,执行强制回滚');
_rollbackToOriginalState(); // 复用现有的回滚方法
}
isEditingNickname.value = false;
}
// 更新用户信息到后端
Future<void> _syncWithBackend() async {
try {

68
lib/modules/profile/views/profile_meeting_view.dart

@ -34,7 +34,10 @@ class ProfileMeetingView extends GetView<ProfileController> {
color: isDarkMode ? Colors.white : Colors.black87,
size: 20.w,
),
onPressed: () => Get.back(),
onPressed: () {
controller.forceRollbackIfUnsaved();
Get.back();
},
),
title: Text(
'账号和资料',
@ -92,35 +95,40 @@ class ProfileMeetingView extends GetView<ProfileController> {
}
Widget _buildBody(bool isDarkMode) {
return SafeArea(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 16.w),
children: [
SizedBox(height: 4.h),
_buildAvatarSection(isDarkMode),
SizedBox(height: 16.h),
_buildNicknameItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildLoginMethodItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildManageLoginItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
SizedBox(height: 32.h),
_buildLogoutButton(isDarkMode),
SizedBox(height: 16.h),
_buildDeleteAccountButton(isDarkMode),
SizedBox(height: 8.h),
_buildWarningText(),
SizedBox(height: 32.h),
],
),
);
return WillPopScope(
onWillPop: () async {
controller.forceRollbackIfUnsaved();
return true;
},
child: SafeArea(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 16.w),
children: [
SizedBox(height: 4.h),
_buildAvatarSection(isDarkMode),
SizedBox(height: 16.h),
_buildNicknameItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildLoginMethodItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildManageLoginItem(isDarkMode),
Divider(
height: 0.5.h,
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
SizedBox(height: 32.h),
_buildLogoutButton(isDarkMode),
SizedBox(height: 16.h),
_buildDeleteAccountButton(isDarkMode),
SizedBox(height: 8.h),
_buildWarningText(),
SizedBox(height: 32.h),
],
),
));
}
Widget _buildAvatarSection(bool isDarkMode) {

31
lib/modules/profile/views/profile_view.dart

@ -158,7 +158,10 @@ class EditProfileView extends GetView<ProfileController> {
size: 20.sp,
color: isDarkMode ? Colors.white : Colors.black,
),
onPressed: () => Get.back(),
onPressed: () {
controller.forceRollbackIfUnsaved();
Get.back();
},
),
// title: Text(
// 'editProfile'.tr,
@ -214,15 +217,23 @@ class EditProfileView extends GetView<ProfileController> {
))
],
),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
_buildEditProfileSection(context, isDarkMode, nameController),
SizedBox(height: 16.h),
_buildAccountSettings(isDarkMode),
SizedBox(height: 16.h),
])));
// ignore: deprecated_member_use
body: WillPopScope(
onWillPop: () async {
ProfileController.to.forceRollbackIfUnsaved();
return true;
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildEditProfileSection(
context, isDarkMode, nameController),
SizedBox(height: 16.h),
_buildAccountSettings(isDarkMode),
SizedBox(height: 16.h),
]))));
}
// 新增:构建编辑资料区域的方法

Loading…
Cancel
Save