Browse Source

refactor(translation): 重构翻译模式选择器的显示逻辑

移除自动显示模式选择器的延迟逻辑,改为在点击按钮时计算位置显示
恢复模式选择器中的关闭按钮
修复模式选择后自动开始识别的问题
weicu
lxm 1 year ago
parent
commit
e3dd422e28
  1. 12
      lib/modules/translation/controllers/translation_controller.dart
  2. 67
      lib/modules/translation/views/dialogs/translation_mode_selector.dart
  3. 29
      lib/modules/translation/views/translation_view.dart

12
lib/modules/translation/controllers/translation_controller.dart

@ -410,14 +410,12 @@ class TranslationController extends GetxController with WidgetsBindingObserver {
WidgetsBinding.instance.addObserver(this);
// 延迟显示模式选择器,确保页面已经完全加载
WidgetsBinding.instance.addPostFrameCallback((_) async {
// 确保上下文有效
if (Get.context == null || !Get.context!.mounted) return;
// WidgetsBinding.instance.addPostFrameCallback((_) async {
// // 确保上下文有效
// if (Get.context == null || !Get.context!.mounted) return;
// Future.delayed(const Duration(milliseconds: 200), () {
_showModeSelector();
// });
});
// _showModeSelector();
// });
}
/// 预先检查权限

67
lib/modules/translation/views/dialogs/translation_mode_selector.dart

@ -10,22 +10,29 @@ class TranslationModeSelector {
/// 显示翻译模式选择器
static void show(bool isDarkMode, {Offset? anchor}) {
final controller = Get.find<TranslationController>();
// 总是从悬浮按钮位置展开,不依赖传入的anchor参数
final Size screenSize = MediaQuery.of(Get.context!).size;
final Offset floatingButtonPos = controller.floatingButtonPosition.value;
// 计算悬浮按钮的中心点位置(从右下角偏移转换为屏幕坐标)
final double buttonSize = 56.0.w; // 悬浮按钮的大小
final double buttonCenterX =
screenSize.width - floatingButtonPos.dx - buttonSize / 2;
final double buttonCenterY =
screenSize.height - floatingButtonPos.dy - buttonSize / 2;
Alignment alignment;
if (anchor != null) {
// 使用传入的锚点计算对齐点
final double ax = (anchor.dx / screenSize.width) * 2 - 1;
final double ay = (anchor.dy / screenSize.height) * 2 - 1;
alignment = Alignment(ax, ay);
} else {
final Offset floatingButtonPos = controller.floatingButtonPosition.value;
// 计算悬浮按钮的中心点位置(从右下角偏移转换为屏幕坐标)
final double buttonSize = 56.0.w; // 悬浮按钮的大小
final double buttonCenterX =
screenSize.width - floatingButtonPos.dx - buttonSize / 2;
final double buttonCenterY =
screenSize.height - floatingButtonPos.dy - buttonSize / 2;
// 计算缩放对齐点(-1..1)
final double ax = (buttonCenterX / screenSize.width) * 2 - 1;
final double ay = (buttonCenterY / screenSize.height) * 2 - 1;
final Alignment alignment = Alignment(ax, ay);
// 计算缩放对齐点(-1..1)
final double ax = (buttonCenterX / screenSize.width) * 2 - 1;
final double ay = (buttonCenterY / screenSize.height) * 2 - 1;
alignment = Alignment(ax, ay);
}
Get.generalDialog(
barrierLabel: 'translation_mode_selector',
@ -93,7 +100,7 @@ class TranslationModeSelector {
Get.back();
await controller.stopAll();
await controller.changeTranslationMode('simultaneous');
//await controller.startRecognition();
await controller.startRecognition();
},
),
_buildModeItem(
@ -106,7 +113,7 @@ class TranslationModeSelector {
Get.back();
await controller.stopAll();
await controller.changeTranslationMode('faceToFace');
//await controller.startRecognition();
await controller.startRecognition();
},
),
_buildModeItem(
@ -120,7 +127,7 @@ class TranslationModeSelector {
Get.back();
await controller.stopAll();
await controller.changeTranslationMode('audioVideo');
//await controller.startRecognition();
await controller.startRecognition();
}
: null,
),
@ -135,23 +142,23 @@ class TranslationModeSelector {
Get.back();
await controller.stopAll();
await controller.changeTranslationMode('call');
//await controller.startRecognition();
await controller.startRecognition();
}
: null,
),
SizedBox(height: 64.w),
// GestureDetector(
// onTap: () => Get.back(),
// child: Container(
// width: 42,
// height: 42,
// decoration: BoxDecoration(
// color: Colors.black.withValues(alpha: 0.8),
// borderRadius: BorderRadius.circular(50),
// ),
// child: const Icon(Icons.close, color: Colors.white),
// ),
// ),
SizedBox(height: 24.w),
GestureDetector(
onTap: () => Get.back(),
child: Container(
width: 42,
height: 42,
decoration: BoxDecoration(
color: Colors.black.withValues(alpha: 0.8),
borderRadius: BorderRadius.circular(50),
),
child: const Icon(Icons.close, color: Colors.white),
),
),
if (Platform.isAndroid) SizedBox(height: 20.h),
],
),

29
lib/modules/translation/views/translation_view.dart

@ -45,10 +45,10 @@ class TranslationView extends GetView<TranslationController> {
children: [
_buildBody(isDarkMode),
// 可拖拽的悬浮按钮
DraggableFloatingButton(
isDarkMode: isDarkMode,
controller: controller,
),
// DraggableFloatingButton(
// isDarkMode: isDarkMode,
// controller: controller,
// ),
],
),
);
@ -633,7 +633,26 @@ class TranslationView extends GetView<TranslationController> {
if (isRecognizing) {
controller.stopAll();
} else {
controller.startRecognition();
// 获取底部按钮的位置
if (controlBarKey.currentContext != null) {
final RenderBox renderBox =
controlBarKey.currentContext!.findRenderObject()
as RenderBox;
final buttonPosition =
renderBox.localToGlobal(Offset.zero);
final buttonSize = renderBox.size;
// 计算按钮中心点
final Offset anchor = buttonPosition +
Offset(buttonSize.width / 2,
buttonSize.height / 2);
TranslationModeSelector.show(isDarkMode,
anchor: anchor);
//controller.startRecognition();
} else {
TranslationModeSelector.show(isDarkMode);
}
}
},
child: Container(

Loading…
Cancel
Save