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.

549 lines
18 KiB

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/translation_controller.dart';
import '../controllers/translation_history_controller.dart';
import '../controllers/translation_detail_controller.dart';
import 'package:intl/intl.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';
class TranslationDetailView extends GetView<TranslationDetailController> {
final TranslationItem item;
final List<TranslationItem> sessionItems;
final TranslationHistoryController historyController;
const TranslationDetailView({
Key? key,
required this.item,
required this.sessionItems,
required this.historyController,
}) : super(key: key);
@override
Widget build(BuildContext context) {
// 初始化控制器
controller.initialize(item, sessionItems);
return Scaffold(
backgroundColor: Color(0xFFF5F5F8),
appBar: AppBar(
backgroundColor: Color(0xFFF5F5F8),
elevation: 0,
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
size: 20.sp,
),
onPressed: () => Get.back(),
),
title: Text(
'Detail',
style: TextStyle(
color: Colors.black,
fontSize: 24.sp,
fontWeight: FontWeight.bold,
),
),
centerTitle: true,
actions: [
IconButton(
icon: Icon(
Icons.share,
color: Colors.black,
size: 22.sp,
),
onPressed: () {
_shareContent();
},
),
],
),
body: Column(
children: [
// 选项卡按钮
Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
child: Row(
children: [
Expanded(
child: Obx(() => _buildTabButton(
title: '原始记录',
isSelected: controller.selectedTabIndex.value == 0,
onTap: () => controller.switchTab(0),
)),
),
SizedBox(width: 12.w),
Expanded(
child: Obx(() => _buildTabButton(
title: '记录摘要',
isSelected: controller.selectedTabIndex.value == 1,
onTap: () => controller.switchTab(1),
)),
),
],
),
),
// 内容区域
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: Obx(() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 会话时间
Padding(
padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 8.h),
child: Text(
DateFormat('yyyy-MM-dd HH:mm').format(item.timestamp),
style: TextStyle(
fontSize: 14.sp,
fontWeight: FontWeight.w600,
color: Colors.grey[600],
letterSpacing: 1.0,
),
),
),
SizedBox(height: 8.h),
// 根据选中的选项卡显示不同内容
if (controller.selectedTabIndex.value == 0)
// 原始记录 - 显示所有翻译项
...sessionItems.map((sessionItem) => _buildSessionItemWidget(sessionItem)).toList()
else
// 记录总结 - 显示合并后的内容
_buildSummaryWidget(),
SizedBox(height: 24.h),
],
);
}),
),
),
],
),
);
}
// 构建选项卡按钮
Widget _buildTabButton({
required String title,
required bool isSelected,
required VoidCallback onTap,
}) {
return GestureDetector(
onTap: onTap,
child: Container(
height: 40.h,
decoration: BoxDecoration(
color: isSelected ? Colors.blue : Colors.white,
borderRadius: BorderRadius.circular(20.r),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(8),
blurRadius: 4,
offset: Offset(0, 1),
),
],
),
alignment: Alignment.center,
child: Text(
title,
style: TextStyle(
fontSize: 15.sp,
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
color: isSelected ? Colors.white : Colors.black87,
),
),
),
);
}
// 构建会话详情中的翻译项
Widget _buildSessionItemWidget(TranslationItem item) {
return Container(
width: double.infinity,
margin: EdgeInsets.only(bottom: 16.h),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.r),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(8),
blurRadius: 8,
offset: Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 源文本部分
Padding(
padding: EdgeInsets.fromLTRB(20.r, 20.r, 20.r, 10.r),
child: Text(
item.sourceText,
style: TextStyle(
fontSize: 15.sp,
color: Colors.black,
fontWeight: FontWeight.w500,
),
),
),
// 翻译文本部分
Padding(
padding: EdgeInsets.fromLTRB(20.r, 10.r, 20.r, 16.r),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: Text(
item.translatedText,
style: TextStyle(
fontSize: 14.sp,
color: Colors.grey[700],
fontWeight: FontWeight.normal,
),
),
),
SizedBox(width: 12.w),
// 播放按钮
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
controller.playTranslationItem(item);
},
borderRadius: BorderRadius.circular(20.r),
splashColor: Colors.blue.withOpacity(0.2),
child: Container(
width: 28.w,
height: 28.h,
decoration: BoxDecoration(
color: Colors.blue.withAlpha(10),
shape: BoxShape.circle,
),
child: Icon(
Icons.volume_up,
color: Colors.blue,
size: 16.sp,
),
),
),
),
],
),
),
],
),
);
}
// 构建总结视图
Widget _buildSummaryWidget() {
final String combinedSourceText = controller.getCombinedSourceText();
return Column(
children: [
// 摘要内容容器
Container(
width: double.infinity,
margin: EdgeInsets.only(bottom: 16.h),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.r),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(8),
blurRadius: 8,
offset: Offset(0, 2),
),
],
),
child: Obx(() {
if (controller.summaryResult.value.isEmpty) {
// 如果没有摘要,显示提示信息
return Padding(
padding: EdgeInsets.all(24.r),
child: Center(
child: Text(
'点击下方按钮生成会话摘要',
style: TextStyle(
fontSize: 15.sp,
color: Colors.grey[600],
fontStyle: FontStyle.italic,
),
textAlign: TextAlign.center,
),
),
);
} else {
// 如果有摘要,显示摘要内容
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.all(20.r),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'摘要内容',
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
SizedBox(height: 12.h),
Text(
controller.summaryResult.value,
style: TextStyle(
fontSize: 15.sp,
color: Colors.black87,
height: 1.5,
),
),
],
),
),
Divider(
height: 1,
thickness: 0.5,
color: Colors.grey[200],
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.r, vertical: 8.r),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
// 播放按钮
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
controller.playText(controller.summaryResult.value);
},
borderRadius: BorderRadius.circular(20.r),
splashColor: Colors.blue.withOpacity(0.2),
child: Padding(
padding: EdgeInsets.all(8.r),
child: Icon(
Icons.volume_up,
color: Colors.blue,
size: 18.sp,
),
),
),
),
SizedBox(width: 12.w),
// 复制按钮
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
// 复制摘要到剪贴板
Clipboard.setData(ClipboardData(text: controller.summaryResult.value));
Get.snackbar(
'已复制',
'摘要已复制到剪贴板',
snackPosition: SnackPosition.BOTTOM,
duration: Duration(seconds: 2),
);
},
borderRadius: BorderRadius.circular(20.r),
splashColor: Colors.blue.withOpacity(0.2),
child: Padding(
padding: EdgeInsets.all(8.r),
child: Icon(
Icons.copy,
color: Colors.blue,
size: 18.sp,
),
),
),
),
],
),
),
],
);
}
}),
),
// 提取摘要按钮
Obx(() => Container(
width: double.infinity,
margin: EdgeInsets.only(bottom: 16.h),
child: ElevatedButton(
onPressed: controller.isGeneratingSummary.value || controller.summaryResult.value.isNotEmpty
? null
: () => _extractSummary(combinedSourceText),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 12.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
elevation: 0,
disabledBackgroundColor: controller.isGeneratingSummary.value
? Colors.blue.withAlpha(128)
: Colors.grey.withAlpha(128),
),
child: controller.isGeneratingSummary.value
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20.w,
height: 20.h,
child: CircularProgressIndicator(
color: Colors.white,
strokeWidth: 2.0,
),
),
SizedBox(width: 12.w),
Text(
'生成中...',
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
],
)
: Text(
controller.summaryResult.value.isEmpty ? '提取摘要' : '已生成摘要',
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
),
)),
// 重新生成按钮
Obx(() {
if (controller.summaryResult.value.isEmpty || controller.isGeneratingSummary.value) {
return SizedBox.shrink();
}
return Container(
width: double.infinity,
margin: EdgeInsets.only(bottom: 16.h),
child: OutlinedButton(
onPressed: () => _extractSummary(combinedSourceText),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.blue,
side: BorderSide(color: Colors.blue),
padding: EdgeInsets.symmetric(vertical: 12.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
),
child: Text(
'重新生成',
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
),
);
}),
],
);
}
// 提取摘要方法
void _extractSummary(String text) async {
final result = await controller.extractSummary(text);
// 检查是否超时,如果超时则显示重试选项
if (result.contains('超时')) {
Get.dialog(
AlertDialog(
title: Text(
'请求超时',
style: TextStyle(
fontSize: 18.sp,
fontWeight: FontWeight.bold,
),
),
content: Text(
'生成摘要请求超时,可能是网络问题或服务器繁忙。',
style: TextStyle(
fontSize: 16.sp,
),
),
actions: [
TextButton(
onPressed: () => Get.back(),
child: Text(
'取消',
style: TextStyle(
fontSize: 16.sp,
color: Colors.grey[700],
),
),
),
TextButton(
onPressed: () {
Get.back();
// 使用延长的超时时间重试
controller.retryExtractSummaryWithExtendedTimeout(text);
},
child: Text(
'重试',
style: TextStyle(
fontSize: 16.sp,
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.r),
),
),
);
}
}
// 分享内容方法
void _shareContent() {
String shareText = '';
if (controller.selectedTabIndex.value == 0) {
// 分享原始记录
final String sourceText = controller.getCombinedSourceText();
final String translatedText = controller.getCombinedTranslatedText();
shareText = '原文:\n$sourceText\n\n译文:\n$translatedText';
} else {
// 分享摘要
if (controller.summaryResult.value.isNotEmpty) {
shareText = '会话摘要:\n${controller.summaryResult.value}';
} else {
Get.snackbar(
'无法分享',
'请先生成摘要',
snackPosition: SnackPosition.BOTTOM,
duration: Duration(seconds: 2),
);
return;
}
}
// 调用系统分享
Share.share(
shareText,
subject: '翻译会话记录 - ${DateFormat('yyyy-MM-dd HH:mm').format(controller.currentItem.timestamp)}',
);
}
}