Browse Source

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

在用户退出编辑页面时自动检查并回滚未保存的更改,防止数据丢失。修改了编辑资料和会议资料视图,添加了WillPopScope处理返回操作。
weicu
lxm 1 year ago
parent
commit
2e59292dca
  1. 9
      lib/modules/profile/controllers/profile_controller.dart
  2. 14
      lib/modules/profile/views/profile_meeting_view.dart
  3. 23
      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 { Future<void> _syncWithBackend() async {
try { try {

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

@ -34,7 +34,10 @@ class ProfileMeetingView extends GetView<ProfileController> {
color: isDarkMode ? Colors.white : Colors.black87, color: isDarkMode ? Colors.white : Colors.black87,
size: 20.w, size: 20.w,
), ),
onPressed: () => Get.back(), onPressed: () {
controller.forceRollbackIfUnsaved();
Get.back();
},
), ),
title: Text( title: Text(
'账号和资料', '账号和资料',
@ -92,7 +95,12 @@ class ProfileMeetingView extends GetView<ProfileController> {
} }
Widget _buildBody(bool isDarkMode) { Widget _buildBody(bool isDarkMode) {
return SafeArea( return WillPopScope(
onWillPop: () async {
controller.forceRollbackIfUnsaved();
return true;
},
child: SafeArea(
child: ListView( child: ListView(
padding: EdgeInsets.symmetric(horizontal: 16.w), padding: EdgeInsets.symmetric(horizontal: 16.w),
children: [ children: [
@ -120,7 +128,7 @@ class ProfileMeetingView extends GetView<ProfileController> {
SizedBox(height: 32.h), SizedBox(height: 32.h),
], ],
), ),
); ));
} }
Widget _buildAvatarSection(bool isDarkMode) { Widget _buildAvatarSection(bool isDarkMode) {

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

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

Loading…
Cancel
Save