feat: 用药打卡全面重构 - 按次追踪 + 独立打卡页 + 任务区汇总
- 后端用药提醒改为按次粒度(每个服药时间独立判断) - 新增 per-dose 打卡/取消打卡 API - 新增 MedicationCheckInPage 独立打卡页(每药每时间单独toggle) - 用药列表删底部弹窗(编辑/停药),点药品直接进该药打卡页 - 任务区用药汇总为一行(过期待服已服) - 支持隔天(EveryOtherDay)/每周频率判断 - 删历史对话功能(ConversationItem + conversationListProvider + UI)
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/navigation_provider.dart';
|
||||
import '../../../providers/auth_provider.dart';
|
||||
import '../../../providers/chat_provider.dart';
|
||||
import '../../../providers/data_providers.dart';
|
||||
|
||||
@@ -64,7 +65,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
case MessageType.dataConfirm:
|
||||
return _buildDataConfirmCard(context, ref, msg);
|
||||
case MessageType.medicationConfirm:
|
||||
return _buildMedicationConfirmCard(context, msg);
|
||||
return _buildMedicationConfirmCard(context, ref, msg);
|
||||
case MessageType.dietAnalysis:
|
||||
return _buildDietAnalysisCard(context, msg);
|
||||
case MessageType.reportAnalysis:
|
||||
@@ -282,7 +283,6 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
|
||||
Widget _buildDataConfirmCard(BuildContext context, WidgetRef ref, ChatMessage msg) {
|
||||
if (msg.confirmed) return const SizedBox.shrink();
|
||||
final meta = msg.metadata;
|
||||
final metricType = meta?['type'] as String? ?? '';
|
||||
final value = meta?['value'] as String? ?? '';
|
||||
@@ -294,7 +294,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.88),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.92),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
@@ -305,9 +305,10 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
Container(
|
||||
width: double.infinity, padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Color(0xFF4CAF50), Color(0xFF43A047)])),
|
||||
child: const Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||
Icon(Icons.check_circle, size: 18, color: Colors.white), SizedBox(width: 6),
|
||||
Text('数据已记录', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Colors.white)),
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||
const Icon(Icons.info_outline, size: 18, color: Colors.white), const SizedBox(width: 6),
|
||||
Text(metricType == 'exercise' ? '请确认运动计划' : '请确认录入',
|
||||
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Colors.white)),
|
||||
]),
|
||||
),
|
||||
Padding(padding: const EdgeInsets.all(18), child: Column(children: [
|
||||
@@ -335,16 +336,32 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
child: const Row(children: [Icon(Icons.warning_amber_rounded, size: 18, color: Color(0xFFE53935)), SizedBox(width: 8), Expanded(child: Text('数值偏高,建议关注', style: TextStyle(fontSize: 13, color: Color(0xFFE53935), fontWeight: FontWeight.w500)))])),
|
||||
],
|
||||
const SizedBox(height: 18),
|
||||
Row(children: [
|
||||
Expanded(child: _cardFilledBtn('确认', Icons.check, onTap: () {
|
||||
msg.confirmed = true;
|
||||
ref.invalidate(latestHealthProvider);
|
||||
final notifier = ref.read(chatProvider.notifier);
|
||||
notifier.markNeedsRebuild();
|
||||
})),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: _cardOutlineBtn('查看详情', Icons.trending_up_outlined, onTap: () => pushRoute(ref, 'trend', params: {'type': metricType}))),
|
||||
]),
|
||||
if (msg.confirmed)
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: null,
|
||||
icon: const Icon(Icons.check_circle, size: 18),
|
||||
label: const Text('录入成功'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF43A047),
|
||||
foregroundColor: Colors.white,
|
||||
disabledBackgroundColor: const Color(0xFF43A047),
|
||||
disabledForegroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
Row(children: [
|
||||
Expanded(child: _cardFilledBtn('确认录入', Icons.check, onTap: () {
|
||||
msg.confirmed = true;
|
||||
ref.invalidate(latestHealthProvider);
|
||||
final notifier = ref.read(chatProvider.notifier);
|
||||
notifier.markNeedsRebuild();
|
||||
})),
|
||||
]),
|
||||
])),
|
||||
]),
|
||||
),
|
||||
@@ -355,19 +372,19 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
// 3. MedicationConfirmCard — 增强版用药确认卡片
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
|
||||
Widget _buildMedicationConfirmCard(BuildContext context, ChatMessage msg) {
|
||||
Widget _buildMedicationConfirmCard(BuildContext context, WidgetRef ref, ChatMessage msg) {
|
||||
final meta = msg.metadata;
|
||||
final name = meta?['name'] as String? ?? '';
|
||||
final dosage = meta?['dosage'] as String? ?? '';
|
||||
final time = meta?['time'] as String? ?? '';
|
||||
final frequency = meta?['frequency'] as String? ?? '';
|
||||
final remaining = meta?['remaining'] as double? ?? 0.65;
|
||||
final duration = meta?['duration_days'] as int?;
|
||||
|
||||
return Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.88),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.92),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
@@ -417,38 +434,36 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
child: Column(
|
||||
children: [
|
||||
// 服药时间 & 频率
|
||||
_medInfoRow(Icons.schedule_outlined, time.isNotEmpty ? '服药时间:$time' : '待设置'),
|
||||
if (frequency.isNotEmpty) _medInfoRow(Icons.repeat, '频率:$frequency'),
|
||||
|
||||
// 剩余药量进度条
|
||||
const SizedBox(height: 14),
|
||||
Row(
|
||||
children: [
|
||||
const Text('剩余药量', style: TextStyle(fontSize: 13, color: Color(0xFF666666))),
|
||||
const Spacer(),
|
||||
Text('${(remaining * 100).toInt()}%', style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: Color(0xFF8B9CF7))),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: LinearProgressIndicator(
|
||||
value: remaining,
|
||||
minHeight: 8,
|
||||
backgroundColor: const Color(0xFFEFEDFF),
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF8B9CF7)),
|
||||
),
|
||||
),
|
||||
|
||||
// 操作按钮
|
||||
const SizedBox(height: 18),
|
||||
Row(children: [
|
||||
Expanded(child: _cardFilledBtn('确认服药', Icons.check_circle_outline)),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: _cardOutlineBtn('跳过', Icons.skip_next)),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: _cardOutlineBtn('设置提醒', Icons.notifications_none_outlined)),
|
||||
]),
|
||||
_medInfoRow(Icons.schedule_outlined, time.isNotEmpty ? '服药时间:$time' : ''),
|
||||
if (frequency.isNotEmpty) _medInfoRow(Icons.repeat, '频率:${_freqLabel(frequency)}'),
|
||||
if (duration != null && duration > 0)
|
||||
_medInfoRow(Icons.calendar_today_outlined, '服用天数:$duration 天'),
|
||||
const SizedBox(height: 16),
|
||||
if (msg.confirmed)
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: null,
|
||||
icon: const Icon(Icons.check_circle, size: 18),
|
||||
label: const Text('录入成功'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF43A047),
|
||||
foregroundColor: Colors.white,
|
||||
disabledBackgroundColor: const Color(0xFF43A047),
|
||||
disabledForegroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
SizedBox(width: double.infinity, child: _cardFilledBtn('确认录入', Icons.check_circle_outlined,
|
||||
onTap: () {
|
||||
msg.confirmed = true;
|
||||
ref.invalidate(medicationListProvider);
|
||||
final notifier = ref.read(chatProvider.notifier);
|
||||
notifier.markNeedsRebuild();
|
||||
})),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
),
|
||||
@@ -472,6 +487,15 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
);
|
||||
}
|
||||
|
||||
String _freqLabel(String freq) {
|
||||
switch (freq.toLowerCase()) {
|
||||
case 'daily': return '每天';
|
||||
case 'everyotherday': return '隔天';
|
||||
case 'weekly': return '每周';
|
||||
default: return freq;
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
// 4. DietAnalysisCard — 增强版饮食分析卡片
|
||||
// ═══════════════════════════════════════════════════════════
|
||||
@@ -486,7 +510,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.88),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.92),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
@@ -578,7 +602,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.88),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.92),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
@@ -758,7 +782,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.88),
|
||||
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.92),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
@@ -857,7 +881,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
children: [
|
||||
// 文字内容
|
||||
if (isUser)
|
||||
Text(msg.content, style: const TextStyle(fontSize: 16, color: Colors.white, height: 1.4))
|
||||
SelectableText(msg.content, style: const TextStyle(fontSize: 16, color: Colors.white, height: 1.4))
|
||||
else
|
||||
MarkdownBody(
|
||||
data: msg.content,
|
||||
@@ -994,6 +1018,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
case 'glucose': return 'mmol/L';
|
||||
case 'spo2': return '%';
|
||||
case 'weight': return 'kg';
|
||||
case 'exercise': return '分钟/天';
|
||||
default: return '';
|
||||
}
|
||||
}
|
||||
@@ -1005,6 +1030,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
case 'glucose': return '💉';
|
||||
case 'spo2': return '🫁';
|
||||
case 'weight': return '⚖️';
|
||||
case 'exercise': return '🏃';
|
||||
default: return '📊';
|
||||
}
|
||||
}
|
||||
@@ -1016,13 +1042,14 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
case 'glucose': return '血糖';
|
||||
case 'spo2': return '血氧';
|
||||
case 'weight': return '体重';
|
||||
case 'exercise': return '运动计划';
|
||||
default: return '健康指标';
|
||||
}
|
||||
}
|
||||
|
||||
static void _medicationCheckIn(WidgetRef ref, BuildContext context) async {
|
||||
try {
|
||||
final service = ref.read(medicationServiceProvider);
|
||||
final api = ref.read(apiClientProvider);
|
||||
final reminders = await ref.read(medicationReminderProvider.future);
|
||||
if (reminders.isEmpty) {
|
||||
if (!context.mounted) return;
|
||||
@@ -1032,7 +1059,10 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
return;
|
||||
}
|
||||
for (final m in reminders) {
|
||||
await service.confirm(m['id']?.toString() ?? '');
|
||||
final id = m['id']?.toString() ?? '';
|
||||
final time = m['scheduledTime']?.toString() ?? '';
|
||||
if (id.isEmpty) continue;
|
||||
await api.post('/api/medications/$id/confirm-dose', data: {'scheduledTime': time, 'status': 'taken'});
|
||||
}
|
||||
ref.invalidate(medicationReminderProvider);
|
||||
if (!context.mounted) return;
|
||||
@@ -1050,6 +1080,7 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static _AgentColors _agentColors(ActiveAgent agent) {
|
||||
return switch (agent) {
|
||||
ActiveAgent.consultation => _AgentColors(
|
||||
@@ -1166,34 +1197,27 @@ class ChatMessagesView extends ConsumerWidget {
|
||||
].join(' · '), status: 'done', onTap: () => pushRoute(ref, 'trend')));
|
||||
}
|
||||
|
||||
// 2. 用药提醒
|
||||
// 2. 用药提醒 — 一行汇总
|
||||
reminders.whenOrNull(data: (meds) {
|
||||
for (final m in meds) {
|
||||
final name = m['name'] ?? '';
|
||||
final dosage = m['dosage'] ?? '';
|
||||
final times = (m['timeOfDay'] as List?)?.map((t) => t.toString().substring(0, 5)).join(', ') ?? '';
|
||||
// 判断该用药时间是否已过
|
||||
bool overdue = false;
|
||||
if (m['timeOfDay'] is List) {
|
||||
final nowTime = TimeOfDay.fromDateTime(now);
|
||||
for (final t in (m['timeOfDay'] as List)) {
|
||||
final parts = t.toString().split(':');
|
||||
final h = int.tryParse(parts[0]) ?? 0;
|
||||
final min = int.tryParse(parts.length > 1 ? parts[1] : '0') ?? 0;
|
||||
if (nowTime.hour > h || (nowTime.hour == h && nowTime.minute > min)) {
|
||||
overdue = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
final overdue = meds.where((m) => m['status'] == 'overdue').length;
|
||||
final upcoming = meds.where((m) => m['status'] == 'upcoming').length;
|
||||
final taken = meds.where((m) => m['status'] == 'taken').length;
|
||||
final total = overdue + upcoming + taken;
|
||||
if (total > 0) {
|
||||
final parts = <String>[];
|
||||
if (overdue > 0) parts.add('$overdue项过期');
|
||||
if (upcoming > 0) parts.add('$upcoming项待服');
|
||||
if (taken > 0) parts.add('$taken项已服');
|
||||
tasks.add(_taskRow(
|
||||
context, Icons.medication_rounded, '$name $dosage ($times)',
|
||||
status: overdue ? 'overdue' : 'pending',
|
||||
onTap: () {},
|
||||
context, Icons.medication_rounded, '用药打卡 ($total项)',
|
||||
trailing: parts.join(' · '),
|
||||
status: overdue > 0 ? 'overdue' : upcoming > 0 ? 'pending' : 'done',
|
||||
onTap: () => pushRoute(ref, 'medCheckIn'),
|
||||
));
|
||||
}
|
||||
});
|
||||
if (tasks.length <= (allNull ? 0 : 1)) {
|
||||
final hasMeds = reminders.asData?.value != null && (reminders.asData!.value).isNotEmpty;
|
||||
if (!hasMeds) {
|
||||
tasks.add(_taskRow(context, Icons.medication_rounded, '暂无用药提醒', status: 'pending'));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user