feat: AI 对话附件上下文解析 + 历史会话归档 + 多页面 UI 重构
- 后端: 新增 AttachmentContextBuilder 解析图片/PDF 摘要并拼入 LLM 上下文; ai_chat_endpoints 扩展附件接口; 新增 ReportAnalysisService - 前端: 新增历史会话页与 conversation_history_provider; chat 链路支持附件展示与回放 - UI: 重构 medication_checkin / notification_center / profile / health_drawer 等多页面 - 配置: api_client baseUrl 适配当前 WiFi IP
This commit is contained in:
352
health_app/lib/pages/history/conversation_history_page.dart
Normal file
352
health_app/lib/pages/history/conversation_history_page.dart
Normal file
@@ -0,0 +1,352 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/app_colors.dart';
|
||||
import '../../core/app_theme.dart';
|
||||
import '../../core/navigation_provider.dart';
|
||||
import '../../providers/chat_provider.dart';
|
||||
import '../../providers/conversation_history_provider.dart';
|
||||
|
||||
/// 对话历史完整列表页。
|
||||
/// - 左滑删除(真删,删后 ScaffoldMessenger 提示)
|
||||
/// - 顶部"清空全部"按钮(确认弹窗 → 调 deleteAll)
|
||||
/// - 点击一条 → loadConversation → popRoute 回首页
|
||||
class ConversationHistoryPage extends ConsumerWidget {
|
||||
const ConversationHistoryPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final async = ref.watch(conversationHistoryProvider);
|
||||
|
||||
return GradientScaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Colors.white.withValues(alpha: 0.9),
|
||||
title: const Text('最近 7 次对话'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => popRoute(ref),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => _confirmClearAll(context, ref),
|
||||
child: const Text(
|
||||
'清空',
|
||||
style: TextStyle(
|
||||
color: AppColors.error,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () =>
|
||||
ref.read(conversationHistoryProvider.notifier).refresh(),
|
||||
child: async.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (e, _) => _ErrorBlock(
|
||||
message: '历史记录加载失败',
|
||||
onRetry: () =>
|
||||
ref.read(conversationHistoryProvider.notifier).refresh(),
|
||||
),
|
||||
data: (list) => list.isEmpty
|
||||
? const _EmptyHint()
|
||||
: ListView.separated(
|
||||
padding: const EdgeInsets.fromLTRB(16, 14, 16, 24),
|
||||
itemCount: list.length,
|
||||
separatorBuilder: (_, index) => const SizedBox(height: 10),
|
||||
itemBuilder: (context, index) {
|
||||
final item = list[index];
|
||||
return _HistoryTile(
|
||||
item: item,
|
||||
onTap: () => _openConversation(ref, item.id),
|
||||
onDelete: () => _deleteOne(context, ref, item.id),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openConversation(WidgetRef ref, String id) async {
|
||||
await ref.read(chatProvider.notifier).loadConversation(id);
|
||||
popRoute(ref);
|
||||
}
|
||||
|
||||
Future<void> _deleteOne(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
String id,
|
||||
) async {
|
||||
try {
|
||||
await ref.read(conversationHistoryProvider.notifier).deleteOne(id);
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('已删除'), duration: Duration(seconds: 1)),
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('删除失败,请稍后重试'),
|
||||
backgroundColor: AppColors.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _confirmClearAll(BuildContext context, WidgetRef ref) async {
|
||||
final ok = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('清空全部历史'),
|
||||
content: const Text('清空后无法恢复,确认继续?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, false),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, true),
|
||||
style: TextButton.styleFrom(foregroundColor: AppColors.error),
|
||||
child: const Text('清空'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (ok != true) return;
|
||||
try {
|
||||
final count = await ref
|
||||
.read(conversationHistoryProvider.notifier)
|
||||
.clearAll();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('已清空 $count 条对话')));
|
||||
}
|
||||
} catch (_) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('清空失败,请稍后重试'),
|
||||
backgroundColor: AppColors.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _HistoryTile extends StatelessWidget {
|
||||
final ConversationListItem item;
|
||||
final VoidCallback onTap;
|
||||
final VoidCallback onDelete;
|
||||
const _HistoryTile({
|
||||
required this.item,
|
||||
required this.onTap,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dismissible(
|
||||
key: ValueKey(item.id),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 22),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.error,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
child: const Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.delete_outline, color: Colors.white),
|
||||
SizedBox(width: 6),
|
||||
Text(
|
||||
'删除',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
confirmDismiss: (_) async {
|
||||
onDelete();
|
||||
return true;
|
||||
},
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.borderLight),
|
||||
boxShadow: AppColors.cardShadowLight,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 38,
|
||||
height: 38,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFEFF6FF),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.forum_rounded,
|
||||
color: Color(0xFF2563EB),
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
_displaySummary(item),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
if (_displayOriginalQuestion(item).isNotEmpty) ...[
|
||||
Text(
|
||||
'首问:${_displayOriginalQuestion(item)}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textHint,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
],
|
||||
Text(
|
||||
'${item.messageCount} 条 · ${_relativeTime(item.updatedAt)}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textHint,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static String _displayTitle(ConversationListItem item) {
|
||||
final s = item.summary?.trim();
|
||||
if (s != null && s.isNotEmpty) return s;
|
||||
final t = item.title?.trim();
|
||||
if (t != null && t.isNotEmpty) return t;
|
||||
return '未命名对话';
|
||||
}
|
||||
|
||||
static String _displaySummary(ConversationListItem item) {
|
||||
final s = item.summary?.trim();
|
||||
if (s != null && s.isNotEmpty) return s.replaceAll(RegExp(r'\s+'), ' ');
|
||||
return _displayTitle(item);
|
||||
}
|
||||
|
||||
static String _displayOriginalQuestion(ConversationListItem item) {
|
||||
final t = item.title?.trim();
|
||||
if (t == null || t.isEmpty) return '';
|
||||
final summary = item.summary?.trim();
|
||||
if (summary != null && summary.isNotEmpty && summary != t) {
|
||||
return t.replaceAll(RegExp(r'\s+'), ' ');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
static String _relativeTime(DateTime time) {
|
||||
final now = DateTime.now();
|
||||
final local = time.toLocal();
|
||||
final diff = now.difference(local);
|
||||
if (diff.inMinutes < 1) return '刚刚';
|
||||
if (diff.inHours < 1) return '${diff.inMinutes} 分钟前';
|
||||
if (diff.inDays < 1) return '${diff.inHours} 小时前';
|
||||
if (diff.inDays < 7) return '${diff.inDays} 天前';
|
||||
return '${local.year}/${local.month}/${local.day}';
|
||||
}
|
||||
}
|
||||
|
||||
class _EmptyHint extends StatelessWidget {
|
||||
const _EmptyHint();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
children: const [
|
||||
SizedBox(height: 120),
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.forum_outlined, size: 56, color: AppColors.textHint),
|
||||
SizedBox(height: 14),
|
||||
Text(
|
||||
'暂无历史对话',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 6),
|
||||
Text(
|
||||
'在首页和 AI 健康助手聊聊吧',
|
||||
style: TextStyle(fontSize: 13, color: AppColors.textSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ErrorBlock extends StatelessWidget {
|
||||
final String message;
|
||||
final VoidCallback onRetry;
|
||||
const _ErrorBlock({required this.message, required this.onRetry});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.wifi_off_rounded,
|
||||
size: 56,
|
||||
color: AppColors.textHint,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(message, style: const TextStyle(color: AppColors.textSecondary)),
|
||||
const SizedBox(height: 14),
|
||||
OutlinedButton(onPressed: onRetry, child: const Text('重试')),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user