You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
403 lines
13 KiB
403 lines
13 KiB
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import '../controllers/text_translation_controller.dart';
|
|
|
|
/// 文本翻译页面
|
|
class TextTranslationView extends GetView<TextTranslationController> {
|
|
const TextTranslationView({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
return Scaffold(
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.grey[50],
|
|
appBar: _buildAppBar(isDarkMode),
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: EdgeInsets.all(16.w),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// 语言选择区域
|
|
_buildLanguageSelector(isDarkMode),
|
|
SizedBox(height: 20.h),
|
|
|
|
// 输入区域
|
|
_buildInputSection(isDarkMode),
|
|
SizedBox(height: 20.h),
|
|
|
|
// 翻译按钮
|
|
_buildTranslateButton(isDarkMode),
|
|
SizedBox(height: 20.h),
|
|
|
|
// 翻译结果区域
|
|
_buildResultSection(isDarkMode),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 构建应用栏
|
|
PreferredSizeWidget _buildAppBar(bool isDarkMode) {
|
|
return AppBar(
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.white,
|
|
elevation: 0,
|
|
leading: IconButton(
|
|
icon: Icon(
|
|
Icons.arrow_back_ios,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
size: 20.sp,
|
|
),
|
|
onPressed: () => Get.back(),
|
|
),
|
|
centerTitle: true,
|
|
title: Text(
|
|
'textTranslation'.tr,
|
|
style: TextStyle(
|
|
fontSize: 18.sp,
|
|
fontWeight: FontWeight.w600,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
),
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.clear_all,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
),
|
|
onPressed: controller.clearInput,
|
|
tooltip: 'clearInput'.tr,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// 构建语言选择器
|
|
Widget _buildLanguageSelector(bool isDarkMode) {
|
|
return Container(
|
|
padding: EdgeInsets.all(16.w),
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[900] : Colors.white,
|
|
borderRadius: BorderRadius.circular(12.r),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.05),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
// 源语言选择
|
|
Obx(() => _buildLanguageButton(
|
|
label: 'sourceLanguage'.tr,
|
|
selectedLanguage: controller.sourceLanguageChineseName.value,
|
|
onTap: () =>
|
|
controller.showLanguageSelector(isSourceLanguage: true),
|
|
isDarkMode: isDarkMode,
|
|
)),
|
|
|
|
SizedBox(height: 12.h),
|
|
|
|
// 交换语言按钮
|
|
Center(
|
|
child: GestureDetector(
|
|
onTap: controller.swapLanguages,
|
|
child: Container(
|
|
width: 40.w,
|
|
height: 40.w,
|
|
decoration: BoxDecoration(
|
|
color: Colors.blue.withOpacity(0.1),
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: Icon(
|
|
Icons.swap_vert,
|
|
color: Colors.blue,
|
|
size: 20.sp,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
SizedBox(height: 12.h),
|
|
|
|
// 目标语言选择
|
|
Obx(() => _buildLanguageButton(
|
|
label: 'targetLanguage'.tr,
|
|
selectedLanguage: controller.targetLanguageChineseName.value,
|
|
onTap: () =>
|
|
controller.showLanguageSelector(isSourceLanguage: false),
|
|
isDarkMode: isDarkMode,
|
|
)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 构建语言选择按钮
|
|
Widget _buildLanguageButton({
|
|
required String label,
|
|
required String selectedLanguage,
|
|
required VoidCallback onTap,
|
|
required bool isDarkMode,
|
|
}) {
|
|
return GestureDetector(
|
|
onTap: onTap,
|
|
child: Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[800] : Colors.grey[100],
|
|
borderRadius: BorderRadius.circular(8.r),
|
|
border: Border.all(
|
|
color: isDarkMode ? Colors.grey[700]! : Colors.grey[300]!,
|
|
),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
label,
|
|
style: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
|
|
),
|
|
),
|
|
SizedBox(height: 4.h),
|
|
Text(
|
|
selectedLanguage,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Icon(
|
|
Icons.keyboard_arrow_down,
|
|
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 构建输入区域
|
|
Widget _buildInputSection(bool isDarkMode) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[900] : Colors.white,
|
|
borderRadius: BorderRadius.circular(12.r),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.05),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 8.h),
|
|
child: Text(
|
|
'inputTextToTranslate'.tr,
|
|
style: TextStyle(
|
|
fontSize: 14.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.grey[300] : Colors.grey[700],
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.symmetric(horizontal: 16.w),
|
|
padding: EdgeInsets.all(12.w),
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[800] : Colors.grey[50],
|
|
borderRadius: BorderRadius.circular(8.r),
|
|
border: Border.all(
|
|
color: isDarkMode ? Colors.grey[700]! : Colors.grey[300]!,
|
|
),
|
|
),
|
|
child: TextField(
|
|
controller: controller.textController,
|
|
maxLines: 6,
|
|
maxLength: 1000,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
),
|
|
decoration: InputDecoration(
|
|
hintText: 'inputTextToTranslate'.tr,
|
|
hintStyle: TextStyle(
|
|
color: isDarkMode ? Colors.grey[500] : Colors.grey[400],
|
|
),
|
|
border: InputBorder.none,
|
|
counterStyle: TextStyle(
|
|
color: isDarkMode ? Colors.grey[500] : Colors.grey[400],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 16.h),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 构建翻译按钮
|
|
Widget _buildTranslateButton(bool isDarkMode) {
|
|
return Obx(() => SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
onPressed: controller.isTranslating.value
|
|
? null
|
|
: controller.translateText,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.blue,
|
|
foregroundColor: Colors.white,
|
|
padding: EdgeInsets.symmetric(vertical: 16.h),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12.r),
|
|
),
|
|
elevation: 0,
|
|
disabledBackgroundColor: Colors.grey,
|
|
),
|
|
child: controller.isTranslating.value
|
|
? Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
SizedBox(
|
|
width: 20.w,
|
|
height: 20.w,
|
|
child: const CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor:
|
|
AlwaysStoppedAnimation<Color>(Colors.white),
|
|
),
|
|
),
|
|
SizedBox(width: 12.w),
|
|
Text(
|
|
'translating'.tr,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: Text(
|
|
'translate'.tr,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
));
|
|
}
|
|
|
|
/// 构建翻译结果区域
|
|
Widget _buildResultSection(bool isDarkMode) {
|
|
return Obx(() {
|
|
if (controller.translatedText.value.isEmpty) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: EdgeInsets.all(16.w),
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[900] : Colors.white,
|
|
borderRadius: BorderRadius.circular(12.r),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.05),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
'translationResult'.tr,
|
|
style: TextStyle(
|
|
fontSize: 14.sp,
|
|
fontWeight: FontWeight.w500,
|
|
color: isDarkMode ? Colors.grey[300] : Colors.grey[700],
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: controller.copyResult,
|
|
child: Container(
|
|
padding:
|
|
EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h),
|
|
decoration: BoxDecoration(
|
|
color: Colors.blue.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(6.r),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(
|
|
Icons.copy,
|
|
size: 16.sp,
|
|
color: Colors.blue,
|
|
),
|
|
SizedBox(width: 4.w),
|
|
Text(
|
|
'copyResult'.tr,
|
|
style: TextStyle(
|
|
fontSize: 12.sp,
|
|
color: Colors.blue,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SizedBox(height: 12.h),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: EdgeInsets.all(12.w),
|
|
decoration: BoxDecoration(
|
|
color: isDarkMode ? Colors.grey[800] : Colors.grey[50],
|
|
borderRadius: BorderRadius.circular(8.r),
|
|
border: Border.all(
|
|
color: isDarkMode ? Colors.grey[700]! : Colors.grey[300]!,
|
|
),
|
|
),
|
|
child: Text(
|
|
controller.translatedText.value,
|
|
style: TextStyle(
|
|
fontSize: 16.sp,
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|