Browse Source

refactor(translation): 优化权限检查逻辑和悬浮按钮拖拽处理

修复Android 11+版本存储权限检查直接返回的问题
将悬浮按钮拖拽逻辑提取为独立方法并增加安全区域检查
在调试模式下显示opus解码测试选项
weicu
lxm 1 year ago
parent
commit
8194abeb58
  1. 37
      lib/modules/settings/views/settings_view.dart
  2. 2
      lib/modules/translation/controllers/translation_list_controller.dart
  3. 36
      lib/modules/translation/views/widgets/draggable_floating_button.dart

37
lib/modules/settings/views/settings_view.dart

@ -515,22 +515,27 @@ class SettingsView extends GetView<SettingsController> {
// ? Colors.white.withOpacity(0.1)
// : Colors.grey[200]),
// //BLE测试
// _buildSimpleNavigationSetting(
// title: 'opus解码测试',
// subtitle: '测试opus解码',
// icon: Icons.bluetooth_searching,
// iconBgColor: isDarkMode
// ? Colors.green[900]!.withOpacity(0.3)
// : Colors.green[100]!,
// iconColor:
// isDarkMode ? Colors.green[300]! : Colors.green[600]!,
// titleColor: isDarkMode ? Colors.white : null,
// subtitleColor: isDarkMode ? Colors.white70 : null,
// onTap: () {
// Get.toNamed(Routes.opusTest);
// },
// isDarkMode: isDarkMode,
// ),
Obx(() => controller.showDebugInfo.value
? _buildSimpleNavigationSetting(
title: 'opus解码测试',
subtitle: '测试opus解码',
icon: Icons.bluetooth_searching,
iconBgColor: isDarkMode
? Colors.green[900]!.withOpacity(0.3)
: Colors.green[100]!,
iconColor: isDarkMode
? Colors.green[300]!
: Colors.green[600]!,
titleColor: isDarkMode ? Colors.white : null,
subtitleColor: isDarkMode ? Colors.white70 : null,
onTap: () {
Get.toNamed(Routes.opusTest);
},
isDarkMode: isDarkMode,
)
: const SizedBox.shrink()),
// Divider(
// height: 1,
// color: isDarkMode

2
lib/modules/translation/controllers/translation_list_controller.dart

@ -104,7 +104,7 @@ class TranslationListController extends GetxController {
// Android 11+ (API 30+) 不需要存储权限
if (sdkVersion >= 30) {
Logger.d("Permission", "Android 11+版本,无需请求存储权限");
//return true; // 直接返回true,表示可以使用存储
return; // 直接返回,表示可以使用存储
}
} catch (e) {
Logger.e("Permission", "获取Android版本信息失败: $e");

36
lib/modules/translation/views/widgets/draggable_floating_button.dart

@ -55,19 +55,7 @@ class DraggableFloatingButton extends StatelessWidget {
),
),
onDragEnd: (details) {
// 计算新位置(相对于屏幕右下角)
final screenSize = MediaQuery.of(Get.context!).size;
final newRight = screenSize.width - details.offset.dx - 56.w;
final newBottom = screenSize.height - details.offset.dy - 56.w;
// 边界检查,确保按钮不会超出屏幕
final clampedRight = newRight.clamp(0.0, screenSize.width - 56.w);
final clampedBottom =
newBottom.clamp(0.0, screenSize.height - 56.w);
// 保存新位置
controller.saveFloatingButtonPosition(
Offset(clampedRight, clampedBottom));
_handleDragEnd(context, details);
},
child: GestureDetector(
onTap: () => _showTranslationModeSelector(isDarkMode),
@ -96,6 +84,28 @@ class DraggableFloatingButton extends StatelessWidget {
));
}
/// 处理拖拽结束事件
void _handleDragEnd(BuildContext context, DraggableDetails details) {
final screenSize = MediaQuery.of(context).size;
final appBarHeight = Scaffold.of(context).appBarMaxHeight ?? kToolbarHeight;
final statusBarHeight = MediaQuery.of(context).padding.top;
final safeAreaTop = statusBarHeight + appBarHeight;
// 计算新位置(相对于屏幕右下角)
final newRight = screenSize.width - details.offset.dx - 56.w;
final newBottom = screenSize.height - details.offset.dy - 56.w;
// 边界检查,确保按钮不会超出屏幕或被AppBar遮盖
final clampedRight = newRight.clamp(0.0, screenSize.width - 56.w);
final clampedBottom = newBottom.clamp(
0.0, // 底部边界
screenSize.height - safeAreaTop - 56.w, // 顶部边界(避免被AppBar遮盖)
);
// 保存新位置
controller.saveFloatingButtonPosition(Offset(clampedRight, clampedBottom));
}
/// 显示翻译模式选择器
void _showTranslationModeSelector(bool isDarkMode) {
TranslationModeSelector.show(isDarkMode);

Loading…
Cancel
Save