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:
@@ -9,7 +9,7 @@ class DrawerShell extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
width: MediaQuery.of(context).size.width * widthFactor,
|
||||
width: MediaQuery.sizeOf(context).width * widthFactor,
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
child: ClipRRect(
|
||||
|
||||
@@ -18,6 +18,7 @@ class EnterpriseHeader extends StatelessWidget {
|
||||
final Color accent;
|
||||
final List<EnterpriseStat> stats;
|
||||
final Widget? trailing;
|
||||
final bool showIcon;
|
||||
|
||||
const EnterpriseHeader({
|
||||
super.key,
|
||||
@@ -28,23 +29,91 @@ class EnterpriseHeader extends StatelessWidget {
|
||||
required this.accent,
|
||||
this.stats = const [],
|
||||
this.trailing,
|
||||
this.showIcon = true,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
for (var i = 0; i < stats.length; i++) ...[
|
||||
Expanded(
|
||||
child: _HeaderStat(
|
||||
stat: stats[i],
|
||||
color: i.isEven ? color : accent,
|
||||
accent: i.isEven ? accent : color,
|
||||
),
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: AppColors.border, width: 1.1),
|
||||
boxShadow: [AppTheme.shadowLight],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (showIcon) ...[
|
||||
Container(
|
||||
width: 46,
|
||||
height: 46,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [color, accent],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
child: Icon(icon, color: Colors.white, size: 25),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
],
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: AppColors.textPrimary,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
Text(
|
||||
subtitle,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: AppColors.textSecondary,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
height: 1.25,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (trailing != null) ...[const SizedBox(width: 10), trailing!],
|
||||
],
|
||||
),
|
||||
if (i != stats.length - 1) const SizedBox(width: 8),
|
||||
if (stats.isNotEmpty) ...[
|
||||
const SizedBox(height: 14),
|
||||
Row(
|
||||
children: [
|
||||
for (var i = 0; i < stats.length; i++) ...[
|
||||
Expanded(
|
||||
child: _HeaderStat(
|
||||
stat: stats[i],
|
||||
color: i.isEven ? color : accent,
|
||||
accent: i.isEven ? accent : color,
|
||||
),
|
||||
),
|
||||
if (i != stats.length - 1) const SizedBox(width: 8),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -63,8 +132,8 @@ class _HeaderStat extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
constraints: const BoxConstraints(minHeight: 54),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 9),
|
||||
constraints: const BoxConstraints(minHeight: 66),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 11),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
@@ -80,8 +149,8 @@ class _HeaderStat extends StatelessWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
if (stat.icon != null) ...[
|
||||
Icon(stat.icon, color: color, size: 16),
|
||||
const SizedBox(width: 6),
|
||||
Icon(stat.icon, color: color, size: 19),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
Expanded(
|
||||
child: Column(
|
||||
@@ -93,8 +162,8 @@ class _HeaderStat extends StatelessWidget {
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: AppColors.textPrimary,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w800,
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
@@ -104,8 +173,8 @@ class _HeaderStat extends StatelessWidget {
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: AppColors.textSecondary,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w700,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -3,6 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../core/app_colors.dart';
|
||||
import '../core/navigation_provider.dart';
|
||||
import '../providers/auth_provider.dart';
|
||||
import '../providers/chat_provider.dart';
|
||||
import '../providers/conversation_history_provider.dart';
|
||||
import '../providers/data_providers.dart';
|
||||
import 'drawer_shell.dart';
|
||||
|
||||
@@ -29,6 +31,8 @@ class HealthDrawer extends ConsumerWidget {
|
||||
_HealthDashboard(latestHealth: latestHealth, ref: ref),
|
||||
const SizedBox(height: 18),
|
||||
_NavigationSection(ref: ref),
|
||||
const SizedBox(height: 18),
|
||||
_HistorySection(ref: ref),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -607,3 +611,286 @@ class _NavItem {
|
||||
required this.colors,
|
||||
});
|
||||
}
|
||||
|
||||
/// 侧边栏底部:最近 5 条对话历史 + 查看全部 + 清空。
|
||||
class _HistorySection extends ConsumerWidget {
|
||||
final WidgetRef ref;
|
||||
const _HistorySection({required this.ref});
|
||||
|
||||
static const int _previewCount = 5;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef _) {
|
||||
final async = ref.watch(conversationHistoryProvider);
|
||||
|
||||
return _Panel(
|
||||
title: '对话记录',
|
||||
trailing: _HistoryActions(ref: ref, async: async),
|
||||
backgroundGradient: const LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Color(0xFFFFFFFF), Color(0xFFF6F1FF), Color(0xFFEFF6FF)],
|
||||
),
|
||||
child: async.when(
|
||||
loading: () => const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 14),
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 22,
|
||||
height: 22,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
),
|
||||
error: (error, stackTrace) => const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 14),
|
||||
child: Text(
|
||||
'加载失败',
|
||||
style: TextStyle(fontSize: 13, color: AppColors.textSecondary),
|
||||
),
|
||||
),
|
||||
data: (list) {
|
||||
if (list.isEmpty) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
child: Text(
|
||||
'暂无历史,发起对话后会显示在这里',
|
||||
style: TextStyle(fontSize: 13, color: AppColors.textSecondary),
|
||||
),
|
||||
);
|
||||
}
|
||||
final preview = list.take(_previewCount).toList();
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
for (var i = 0; i < preview.length; i++)
|
||||
_DrawerHistoryTile(
|
||||
item: preview[i],
|
||||
isLast: i == preview.length - 1,
|
||||
onTap: () async {
|
||||
Navigator.of(context).maybePop(); // 关侧边栏
|
||||
await ref
|
||||
.read(chatProvider.notifier)
|
||||
.loadConversation(preview[i].id);
|
||||
},
|
||||
onDelete: () async {
|
||||
try {
|
||||
await ref
|
||||
.read(conversationHistoryProvider.notifier)
|
||||
.deleteOne(preview[i].id);
|
||||
} catch (_) {
|
||||
// 失败时 provider 已回滚状态,UI 自然恢复
|
||||
}
|
||||
},
|
||||
),
|
||||
if (list.length > _previewCount) ...[
|
||||
const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).maybePop();
|
||||
pushRoute(ref, 'conversationHistory');
|
||||
},
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
||||
child: Text(
|
||||
'查看全部',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _HistoryActions extends StatelessWidget {
|
||||
final WidgetRef ref;
|
||||
final AsyncValue<List<ConversationListItem>> async;
|
||||
const _HistoryActions({required this.ref, required this.async});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final hasItems = (async.asData?.value.isNotEmpty ?? false);
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
onTap: () => ref.read(conversationHistoryProvider.notifier).refresh(),
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(6),
|
||||
child: Icon(
|
||||
Icons.refresh_rounded,
|
||||
size: 18,
|
||||
color: Color(0xFF6D28D9),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (hasItems)
|
||||
InkWell(
|
||||
onTap: () => _confirmClearAll(context, ref),
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(6),
|
||||
child: Icon(
|
||||
Icons.delete_sweep_rounded,
|
||||
size: 18,
|
||||
color: 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 {
|
||||
await ref.read(conversationHistoryProvider.notifier).clearAll();
|
||||
} catch (_) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('清空失败,请稍后重试'),
|
||||
backgroundColor: AppColors.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _DrawerHistoryTile extends StatelessWidget {
|
||||
final ConversationListItem item;
|
||||
final bool isLast;
|
||||
final VoidCallback onTap;
|
||||
final VoidCallback onDelete;
|
||||
const _DrawerHistoryTile({
|
||||
required this.item,
|
||||
required this.isLast,
|
||||
required this.onTap,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dismissible(
|
||||
key: ValueKey('drawer-${item.id}'),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 14),
|
||||
margin: EdgeInsets.only(bottom: isLast ? 0 : 8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.error,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: const Icon(Icons.delete_outline, color: Colors.white),
|
||||
),
|
||||
confirmDismiss: (_) async {
|
||||
onDelete();
|
||||
return true;
|
||||
},
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: Container(
|
||||
margin: EdgeInsets.only(bottom: isLast ? 0 : 6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.72),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: Colors.white.withValues(alpha: 0.9)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_displaySummary(item),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: AppColors.textPrimary,
|
||||
height: 1.2,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
_shortDate(item.updatedAt),
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w700,
|
||||
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 _shortDate(DateTime time) {
|
||||
final now = DateTime.now();
|
||||
final local = time.toLocal();
|
||||
if (local.year == now.year &&
|
||||
local.month == now.month &&
|
||||
local.day == now.day) {
|
||||
return '今天';
|
||||
}
|
||||
final yesterday = now.subtract(const Duration(days: 1));
|
||||
if (local.year == yesterday.year &&
|
||||
local.month == yesterday.month &&
|
||||
local.day == yesterday.day) {
|
||||
return '昨天';
|
||||
}
|
||||
return '${local.month}/${local.day}';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user