From 2e59292dca36ac337ef0a334bfae70c459142c4a Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Sun, 20 Jul 2025 12:13:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(profile):=20=E6=B7=BB=E5=8A=A0=E9=80=80?= =?UTF-8?q?=E5=87=BA=E6=97=B6=E5=BC=BA=E5=88=B6=E5=9B=9E=E6=BB=9A=E6=9C=AA?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=9B=B4=E6=94=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在用户退出编辑页面时自动检查并回滚未保存的更改,防止数据丢失。修改了编辑资料和会议资料视图,添加了WillPopScope处理返回操作。 --- .../controllers/profile_controller.dart | 9 +++ .../profile/views/profile_meeting_view.dart | 68 +++++++++++-------- lib/modules/profile/views/profile_view.dart | 31 ++++++--- 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/lib/modules/profile/controllers/profile_controller.dart b/lib/modules/profile/controllers/profile_controller.dart index e679fecb7..a26ec587e 100644 --- a/lib/modules/profile/controllers/profile_controller.dart +++ b/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 _syncWithBackend() async { try { diff --git a/lib/modules/profile/views/profile_meeting_view.dart b/lib/modules/profile/views/profile_meeting_view.dart index ccf7d29a1..dcda97711 100644 --- a/lib/modules/profile/views/profile_meeting_view.dart +++ b/lib/modules/profile/views/profile_meeting_view.dart @@ -34,7 +34,10 @@ class ProfileMeetingView extends GetView { 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 { } 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) { diff --git a/lib/modules/profile/views/profile_view.dart b/lib/modules/profile/views/profile_view.dart index dd3f1dea3..6f18f6d9f 100644 --- a/lib/modules/profile/views/profile_view.dart +++ b/lib/modules/profile/views/profile_view.dart @@ -158,7 +158,10 @@ class EditProfileView extends GetView { 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 { )) ], ), - 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), + ])))); } // 新增:构建编辑资料区域的方法