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.addObserver(this);
// 延迟显示模式选择器,确保页面已经完全加载 // 延迟显示模式选择器,确保页面已经完全加载
WidgetsBinding.instance.addPostFrameCallback((_) async { // WidgetsBinding.instance.addPostFrameCallback((_) async {
// 确保上下文有效 // // 确保上下文有效
if (Get.context == null || !Get.context!.mounted) return; // 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}) { static void show(bool isDarkMode, {Offset? anchor}) {
final controller = Get.find<TranslationController>(); final controller = Get.find<TranslationController>();
// 总是从悬浮按钮位置展开,不依赖传入的anchor参数
final Size screenSize = MediaQuery.of(Get.context!).size; final Size screenSize = MediaQuery.of(Get.context!).size;
final Offset floatingButtonPos = controller.floatingButtonPosition.value;
// 计算悬浮按钮的中心点位置(从右下角偏移转换为屏幕坐标) Alignment alignment;
final double buttonSize = 56.0.w; // 悬浮按钮的大小
final double buttonCenterX = if (anchor != null) {
screenSize.width - floatingButtonPos.dx - buttonSize / 2; // 使用传入的锚点计算对齐点
final double buttonCenterY = final double ax = (anchor.dx / screenSize.width) * 2 - 1;
screenSize.height - floatingButtonPos.dy - buttonSize / 2; 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) // 计算缩放对齐点(-1..1)
final double ax = (buttonCenterX / screenSize.width) * 2 - 1; final double ax = (buttonCenterX / screenSize.width) * 2 - 1;
final double ay = (buttonCenterY / screenSize.height) * 2 - 1; final double ay = (buttonCenterY / screenSize.height) * 2 - 1;
final Alignment alignment = Alignment(ax, ay); alignment = Alignment(ax, ay);
}
Get.generalDialog( Get.generalDialog(
barrierLabel: 'translation_mode_selector', barrierLabel: 'translation_mode_selector',
@ -93,7 +100,7 @@ class TranslationModeSelector {
Get.back(); Get.back();
await controller.stopAll(); await controller.stopAll();
await controller.changeTranslationMode('simultaneous'); await controller.changeTranslationMode('simultaneous');
//await controller.startRecognition(); await controller.startRecognition();
}, },
), ),
_buildModeItem( _buildModeItem(
@ -106,7 +113,7 @@ class TranslationModeSelector {
Get.back(); Get.back();
await controller.stopAll(); await controller.stopAll();
await controller.changeTranslationMode('faceToFace'); await controller.changeTranslationMode('faceToFace');
//await controller.startRecognition(); await controller.startRecognition();
}, },
), ),
_buildModeItem( _buildModeItem(
@ -120,7 +127,7 @@ class TranslationModeSelector {
Get.back(); Get.back();
await controller.stopAll(); await controller.stopAll();
await controller.changeTranslationMode('audioVideo'); await controller.changeTranslationMode('audioVideo');
//await controller.startRecognition(); await controller.startRecognition();
} }
: null, : null,
), ),
@ -135,23 +142,23 @@ class TranslationModeSelector {
Get.back(); Get.back();
await controller.stopAll(); await controller.stopAll();
await controller.changeTranslationMode('call'); await controller.changeTranslationMode('call');
//await controller.startRecognition(); await controller.startRecognition();
} }
: null, : null,
), ),
SizedBox(height: 64.w), SizedBox(height: 24.w),
// GestureDetector( GestureDetector(
// onTap: () => Get.back(), onTap: () => Get.back(),
// child: Container( child: Container(
// width: 42, width: 42,
// height: 42, height: 42,
// decoration: BoxDecoration( decoration: BoxDecoration(
// color: Colors.black.withValues(alpha: 0.8), color: Colors.black.withValues(alpha: 0.8),
// borderRadius: BorderRadius.circular(50), borderRadius: BorderRadius.circular(50),
// ), ),
// child: const Icon(Icons.close, color: Colors.white), child: const Icon(Icons.close, color: Colors.white),
// ), ),
// ), ),
if (Platform.isAndroid) SizedBox(height: 20.h), 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: [ children: [
_buildBody(isDarkMode), _buildBody(isDarkMode),
// 可拖拽的悬浮按钮 // 可拖拽的悬浮按钮
DraggableFloatingButton( // DraggableFloatingButton(
isDarkMode: isDarkMode, // isDarkMode: isDarkMode,
controller: controller, // controller: controller,
), // ),
], ],
), ),
); );
@ -633,7 +633,26 @@ class TranslationView extends GetView<TranslationController> {
if (isRecognizing) { if (isRecognizing) {
controller.stopAll(); controller.stopAll();
} else { } 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( child: Container(

Loading…
Cancel
Save