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.

245 lines
6.9 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 '../bindings/translation_detail_binding.dart';
import 'translation_detail_view.dart';
class TranslationHistoryView extends GetView<TranslationHistoryController> {
const TranslationHistoryView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
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(
'History',
style: TextStyle(
color: Colors.black,
fontSize: 24.sp,
fontWeight: FontWeight.bold,
),
),
centerTitle: true,
actions: [
IconButton(
icon: Icon(
Icons.delete_outline,
color: Colors.black,
size: 24.sp,
),
onPressed: () {
_showClearHistoryConfirmDialog();
},
),
],
),
body: Obx(() {
if (!controller.hasHistory) {
return Center(
child: Text(
'暂无历史记录',
style: TextStyle(
fontSize: 16.sp,
color: Colors.grey,
),
),
);
}
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
itemCount: controller.sortedDates.length,
itemBuilder: (context, index) {
final date = controller.sortedDates[index];
final items = controller.groupedHistory.value[date]!;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(top: 24.h, bottom: 12.h, left: 12.w),
child: Text(
date,
style: TextStyle(
fontSize: 14.sp,
fontWeight: FontWeight.w600,
color: Colors.grey[600],
letterSpacing: 1.0,
),
),
),
_buildDateGroupContainer(items),
],
);
},
);
}),
);
}
// 构建日期组容器
Widget _buildDateGroupContainer(List<TranslationItem> items) {
return Container(
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: ListView.separated(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.zero,
itemCount: items.length,
separatorBuilder: (context, index) => Divider(
height: 1,
thickness: 0.5,
color: Colors.grey[200],
indent: 0,
endIndent: 0,
),
itemBuilder: (context, index) {
return _buildHistoryItemInGroup(items[index]);
},
),
);
}
// 构建组内的历史记录项
Widget _buildHistoryItemInGroup(TranslationItem item) {
return InkWell(
onTap: () {
// 点击查看详情
_showTranslationDetail(item);
},
splashColor: Colors.blue.withOpacity(0.1),
highlightColor: Colors.blue.withOpacity(0.05),
borderRadius: BorderRadius.circular(8.r),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 源文本
Text(
item.sourceText,
style: TextStyle(
fontSize: 15.sp,
fontWeight: FontWeight.w500,
color: Colors.black,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
SizedBox(height: 6.h),
// 翻译文本
Text(
item.translatedText,
style: TextStyle(
fontSize: 14.sp,
color: Colors.grey[700],
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
],
),
),
Icon(
Icons.chevron_right,
color: Colors.grey[350],
size: 20.sp,
),
],
),
),
);
}
void _showTranslationDetail(TranslationItem item) {
// 获取该会话的所有翻译项
final List<TranslationItem> sessionItems = controller.getSessionItems(item.sessionId);
Get.to(
() => TranslationDetailView(
item: item,
sessionItems: sessionItems,
historyController: controller,
),
binding: TranslationDetailBinding(),
);
}
void _showClearHistoryConfirmDialog() {
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: () {
// 清除历史记录
controller.translationController.clearTranslationHistory();
Get.back();
},
child: Text(
'确定',
style: TextStyle(
fontSize: 16.sp,
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.r),
),
),
);
}
}