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 { Future<void> _syncWithBackend() async {
try { 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, 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,35 +95,40 @@ class ProfileMeetingView extends GetView<ProfileController> {
} }
Widget _buildBody(bool isDarkMode) { Widget _buildBody(bool isDarkMode) {
return SafeArea( return WillPopScope(
child: ListView( onWillPop: () async {
padding: EdgeInsets.symmetric(horizontal: 16.w), controller.forceRollbackIfUnsaved();
children: [ return true;
SizedBox(height: 4.h), },
_buildAvatarSection(isDarkMode), child: SafeArea(
SizedBox(height: 16.h), child: ListView(
_buildNicknameItem(isDarkMode), padding: EdgeInsets.symmetric(horizontal: 16.w),
Divider( children: [
height: 0.5.h, SizedBox(height: 4.h),
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]), _buildAvatarSection(isDarkMode),
_buildLoginMethodItem(isDarkMode), SizedBox(height: 16.h),
Divider( _buildNicknameItem(isDarkMode),
height: 0.5.h, Divider(
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]), height: 0.5.h,
_buildManageLoginItem(isDarkMode), color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
Divider( _buildLoginMethodItem(isDarkMode),
height: 0.5.h, Divider(
color: isDarkMode ? Colors.grey[800] : Colors.grey[200]), height: 0.5.h,
SizedBox(height: 32.h), color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildLogoutButton(isDarkMode), _buildManageLoginItem(isDarkMode),
SizedBox(height: 16.h), Divider(
_buildDeleteAccountButton(isDarkMode), height: 0.5.h,
SizedBox(height: 8.h), color: isDarkMode ? Colors.grey[800] : Colors.grey[200]),
_buildWarningText(), SizedBox(height: 32.h),
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) { Widget _buildAvatarSection(bool isDarkMode) {

31
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
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h), body: WillPopScope(
child: onWillPop: () async {
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ ProfileController.to.forceRollbackIfUnsaved();
_buildEditProfileSection(context, isDarkMode, nameController), return true;
SizedBox(height: 16.h), },
_buildAccountSettings(isDarkMode), child: Padding(
SizedBox(height: 16.h), 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