523 lines
23 KiB
Dart
523 lines
23 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../core/navigation_provider.dart';
|
|
import '../providers/auth_provider.dart';
|
|
import '../providers/data_providers.dart';
|
|
import '../providers/chat_provider.dart';
|
|
|
|
/// 侧滑抽屉——彩色分区卡片式设计
|
|
class HealthDrawer extends ConsumerWidget {
|
|
const HealthDrawer({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final auth = ref.watch(authProvider);
|
|
final user = auth.user;
|
|
final latestHealth = ref.watch(latestHealthProvider);
|
|
final conversations = ref.watch(conversationListProvider);
|
|
|
|
return Drawer(
|
|
width: MediaQuery.of(context).size.width * 0.82,
|
|
backgroundColor: const Color(0xFFFAFBFE),
|
|
child: SafeArea(
|
|
child: ListView(
|
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 20),
|
|
children: [
|
|
// ════════════ 用户区 ════════════
|
|
_SectionCard(
|
|
color: const Color(0xFF635BFF),
|
|
gradientColors: [const Color(0xFF7C74FF), const Color(0xFF5248E8)],
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(18),
|
|
child: Row(children: [
|
|
GestureDetector(
|
|
onTap: () => pushRoute(ref, 'profile'),
|
|
child: Container(
|
|
width: 52, height: 52,
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(colors: [Colors.white.withAlpha(40), Colors.white.withAlpha(15)]),
|
|
shape: BoxShape.circle,
|
|
border: Border.all(color: Colors.white30, width: 1.5),
|
|
),
|
|
child: user?.avatarUrl != null
|
|
? ClipOval(child: Image.network(user!.avatarUrl!, fit: BoxFit.cover, errorBuilder: (_, e, s) => _defaultAvatar()))
|
|
: _defaultAvatar(),
|
|
),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(user?.name ?? '未设置昵称', style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w700, color: Colors.white)),
|
|
const SizedBox(height: 2),
|
|
Text(user?.phone ?? '未登录', style: TextStyle(fontSize: 12, color: Colors.white70)),
|
|
],
|
|
)),
|
|
Icon(Icons.chevron_right, size: 18, color: Colors.white54),
|
|
]),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
// ════════════ 健康概览区 ════════════
|
|
_SectionCard(
|
|
color: const Color(0xFFE8F0FE),
|
|
gradientColors: null,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 14, 16, 4),
|
|
child: Row(children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF635BFF).withAlpha(15),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
|
Icon(Icons.monitor_heart_rounded, size: 13, color: const Color(0xFF635BFF)),
|
|
SizedBox(width: 4),
|
|
Text('健康概览', style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xFF635BFF))),
|
|
]),
|
|
),
|
|
const Spacer(),
|
|
GestureDetector(
|
|
onTap: () => pushRoute(ref, 'trend'),
|
|
child: const Padding(padding: EdgeInsets.all(4), child: Text('详情', style: TextStyle(fontSize: 11, color: Color(0xFF888888)))),
|
|
),
|
|
]),
|
|
),
|
|
latestHealth.when(
|
|
data: (data) => Padding(
|
|
padding: const EdgeInsets.fromLTRB(12, 8, 12, 14),
|
|
child: Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
_MetricTile(icon: Icons.favorite_rounded, label: '血压', value: _bpText(data['BloodPressure']), accentColor: const Color(0xFFFF6B6B), onTap: () => pushRoute(ref, 'trend', params: {'type': 'blood_pressure'})),
|
|
_MetricTile(icon: Icons.monitor_heart_outlined, label: '心率', value: _metricVal(data['HeartRate']), unit: '', accentColor: const Color(0xFFFF9F43), onTap: () => pushRoute(ref, 'trend', params: {'type': 'heart_rate'})),
|
|
_MetricTile(icon: Icons.bloodtype_outlined, label: '血糖', value: _metricVal(data['Glucose']), unit: '', accentColor: const Color(0xFF26C281), onTap: () => pushRoute(ref, 'trend', params: {'type': 'glucose'})),
|
|
_MetricTile(icon: Icons.air_outlined, label: '血氧', value: _metricVal(data['SpO2']), unit: '%', accentColor: const Color(0xFF4D96FF), onTap: () => pushRoute(ref, 'trend', params: {'type': 'spo2'})),
|
|
_MetricTile(icon: Icons.monitor_weight_outlined, label: '体重', value: _metricVal(data['Weight']), unit: 'kg', accentColor: const Color(0xFFA55EEA), onTap: () => pushRoute(ref, 'trend', params: {'type': 'weight'})),
|
|
],
|
|
),
|
|
),
|
|
loading: () => const Padding(padding: EdgeInsets.symmetric(vertical: 20), child: Center(child: SizedBox(width: 22, height: 22, child: CircularProgressIndicator(strokeWidth: 2, color: Color(0xFF635BFF))))),
|
|
error: (Object err, StackTrace st) => Padding(
|
|
padding: const EdgeInsets.fromLTRB(12, 8, 12, 14),
|
|
child: Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
_MetricTile(icon: Icons.favorite_rounded, label: '血压', value: '--', accentColor: const Color(0xFFFF6B6B)),
|
|
_MetricTile(icon: Icons.monitor_heart_outlined, label: '心率', value: '--', accentColor: const Color(0xFFFF9F43)),
|
|
_MetricTile(icon: Icons.bloodtype_outlined, label: '血糖', value: '--', accentColor: const Color(0xFF26C281)),
|
|
_MetricTile(icon: Icons.air_outlined, label: '血氧', value: '--', accentColor: const Color(0xFF4D96FF)),
|
|
_MetricTile(icon: Icons.monitor_weight_outlined, label: '体重', value: '--', accentColor: const Color(0xFFA55EEA)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
// ════════════ 功能区(横向排布)════════════
|
|
_SectionCard(
|
|
color: const Color(0xFFFDF6EC),
|
|
gradientColors: null,
|
|
child: Padding(
|
|
padding: const EdgeInsets.fromLTRB(14, 12, 14, 14),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: Row(children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFF0A060).withAlpha(15),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: const Row(mainAxisSize: MainAxisSize.min, children: [
|
|
Icon(Icons.apps_rounded, size: 13, color: Color(0xFFF0A060)),
|
|
SizedBox(width: 4),
|
|
Text('功能', style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xFFF0A060))),
|
|
]),
|
|
),
|
|
]),
|
|
),
|
|
Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
_FeatureChip(icon: Icons.description_outlined, label: '报告管理', bgColor: const Color(0xFFFFEDE0), iconColor: const Color(0xFFF0A060), onTap: () => pushRoute(ref, 'reports')),
|
|
_FeatureChip(icon: Icons.calendar_today_outlined, label: '健康日历', bgColor: const Color(0xFFE0F0E0), iconColor: const Color(0xFF26C281), onTap: () => pushRoute(ref, 'calendar')),
|
|
_FeatureChip(icon: Icons.restaurant_outlined, label: '饮食记录', bgColor: const Color(0xFFFFE8E0), iconColor: const Color(0xFFFF8C42), onTap: () => pushRoute(ref, 'dietRecords')),
|
|
_FeatureChip(icon: Icons.event_note_outlined, label: '复查随访', bgColor: const Color(0xFFE8E0FF), iconColor: const Color(0xFF8B6CF7), onTap: () => pushRoute(ref, 'followups')),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
// ════════════ 历史对话区 ════════════
|
|
_SectionCard(
|
|
color: const Color(0xFFF0F4FF),
|
|
gradientColors: null,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 14, 16, 4),
|
|
child: Row(children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF4D96FF).withAlpha(15),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: const Row(mainAxisSize: MainAxisSize.min, children: [
|
|
Icon(Icons.history_rounded, size: 13, color: Color(0xFF4D96FF)),
|
|
SizedBox(width: 4),
|
|
Text('历史对话', style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xFF4D96FF))),
|
|
]),
|
|
),
|
|
const Spacer(),
|
|
GestureDetector(
|
|
onTap: () => ref.invalidate(conversationListProvider),
|
|
child: const Padding(padding: EdgeInsets.all(4), child: Icon(Icons.refresh, size: 15, color: Color(0xFFAAAAAA))),
|
|
),
|
|
]),
|
|
),
|
|
_buildConversationList(ref, conversations),
|
|
],
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
// ════════════ 设置区 ════════════
|
|
_SectionCard(
|
|
color: const Color(0xFFF5F5F7),
|
|
gradientColors: null,
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: () => pushRoute(ref, 'settings'),
|
|
borderRadius: BorderRadius.circular(16),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 14),
|
|
child: Row(children: [
|
|
Container(
|
|
width: 34, height: 34,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFEEEEEE),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: const Icon(Icons.settings_outlined, size: 18, color: Color(0xFF666666)),
|
|
),
|
|
const SizedBox(width: 12),
|
|
const Expanded(child: Text('设置', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500, color: Color(0xFF333333)))),
|
|
const Icon(Icons.chevron_right, size: 16, color: Color(0xFFCCCCCC)),
|
|
]),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 6),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
static Widget _defaultAvatar() => const Icon(Icons.person, size: 26, color: Colors.white70);
|
|
|
|
String _bpText(dynamic bp) {
|
|
if (bp == null) return '--';
|
|
if (bp is Map) return '${bp['systolic'] ?? '--'}/${bp['diastolic'] ?? '--'}';
|
|
return '--';
|
|
}
|
|
|
|
String _metricVal(dynamic metric) {
|
|
if (metric == null) return '--';
|
|
if (metric is Map) { final v = metric['value']; return v?.toString() ?? '--'; }
|
|
return metric.toString();
|
|
}
|
|
|
|
Widget _buildConversationList(WidgetRef ref, AsyncValue<List<ConversationItem>> conversations) {
|
|
return conversations.when(
|
|
data: (items) {
|
|
if (items.isEmpty) {
|
|
return const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 16),
|
|
child: Center(
|
|
child: Text('暂无历史对话', style: TextStyle(color: Color(0xFFBBBBBB), fontSize: 13)),
|
|
),
|
|
);
|
|
}
|
|
return Padding(
|
|
padding: const EdgeInsets.fromLTRB(8, 6, 8, 14),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: items.map((item) => _ConversationItem(item: item)).toList(),
|
|
),
|
|
);
|
|
},
|
|
loading: () => const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 16),
|
|
child: Center(
|
|
child: SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2, color: Color(0xFF4D96FF)),
|
|
),
|
|
),
|
|
),
|
|
error: (Object err, StackTrace st) => const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 16),
|
|
child: Center(
|
|
child: Text('加载失败', style: TextStyle(color: Color(0xFFBBBBBB), fontSize: 13)),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
// 分区卡片容器 —— 带圆角、阴影和微动效
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
class _SectionCard extends StatelessWidget {
|
|
final Widget child;
|
|
final Color color;
|
|
final List<Color>? gradientColors;
|
|
|
|
const _SectionCard({required this.child, required this.color, this.gradientColors});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return TweenAnimationBuilder<double>(
|
|
tween: Tween(begin: 0.0, end: 1.0),
|
|
duration: const Duration(milliseconds: 500),
|
|
curve: Curves.easeOutCubic,
|
|
builder: (context, value, child) => Transform.translate(
|
|
offset: Offset(0, 8 * (1 - value)),
|
|
child: Opacity(opacity: value, child: child),
|
|
),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: gradientColors == null ? color : null,
|
|
gradient: gradientColors != null ? LinearGradient(
|
|
colors: gradientColors!,
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
) : null,
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: (gradientColors?.first ?? color).withAlpha(25),
|
|
blurRadius: 12,
|
|
offset: const Offset(0, 4),
|
|
),
|
|
],
|
|
),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: child,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
// 健康指标小方块
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
class _MetricTile extends StatelessWidget {
|
|
final IconData icon;
|
|
final String label;
|
|
final String value;
|
|
final String? unit;
|
|
final Color accentColor;
|
|
final VoidCallback? onTap;
|
|
|
|
const _MetricTile({
|
|
required this.icon,
|
|
required this.label,
|
|
required this.value,
|
|
this.unit,
|
|
required this.accentColor,
|
|
this.onTap,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: onTap,
|
|
child: Container(
|
|
width: ((MediaQuery.of(context).size.width * 0.82 - 48) / 3).floorToDouble(),
|
|
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 6),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: accentColor.withAlpha(30)),
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
width: 28, height: 28,
|
|
decoration: BoxDecoration(
|
|
color: accentColor.withAlpha(15),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(icon, size: 15, color: accentColor),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(value, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w700, color: const Color(0xFF1A1A1A))),
|
|
Text(label, style: TextStyle(fontSize: 10, color: Colors.grey[500])),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
// 功能按钮(横向)
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
class _FeatureChip extends StatelessWidget {
|
|
final IconData icon;
|
|
final String label;
|
|
final Color bgColor;
|
|
final Color iconColor;
|
|
final VoidCallback onTap;
|
|
|
|
const _FeatureChip({
|
|
required this.icon,
|
|
required this.label,
|
|
required this.bgColor,
|
|
required this.iconColor,
|
|
required this.onTap,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: onTap,
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: AnimatedContainer(
|
|
duration: const Duration(milliseconds: 200),
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
color: bgColor,
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
|
Icon(icon, size: 17, color: iconColor),
|
|
const SizedBox(width: 6),
|
|
Text(label, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500, color: iconColor.withAlpha(220))),
|
|
]),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
// 历史对话项
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
class _ConversationItem extends StatelessWidget {
|
|
final ConversationItem item;
|
|
|
|
const _ConversationItem({required this.item});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final colors = _conversationColors(item.agent);
|
|
return Container(
|
|
margin: const EdgeInsets.symmetric(vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: colors.$1.withAlpha(80)),
|
|
),
|
|
child: ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
|
|
leading: Container(
|
|
width: 32, height: 32,
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(colors: [colors.$2.withAlpha(30), colors.$2.withAlpha(15)]),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(_getAgentIcon(item.agent), size: 15, color: colors.$2),
|
|
),
|
|
title: Text(item.title, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: Color(0xFF333333))),
|
|
subtitle: Text(item.lastMessage, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 11, color: Colors.grey[500])),
|
|
trailing: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(_formatTime(item.updatedAt), style: TextStyle(fontSize: 9, color: Colors.grey[400])),
|
|
const SizedBox(height: 2),
|
|
Icon(Icons.chevron_right, size: 12, color: Colors.grey[300]),
|
|
],
|
|
),
|
|
dense: true,
|
|
visualDensity: VisualDensity.compact,
|
|
),
|
|
);
|
|
}
|
|
|
|
IconData _getAgentIcon(ActiveAgent agent) {
|
|
return switch (agent) {
|
|
ActiveAgent.health => Icons.health_and_safety_outlined,
|
|
ActiveAgent.diet => Icons.restaurant_outlined,
|
|
ActiveAgent.medication => Icons.medication_outlined,
|
|
ActiveAgent.report => Icons.description_outlined,
|
|
ActiveAgent.exercise => Icons.directions_run_outlined,
|
|
ActiveAgent.consultation => Icons.chat_bubble_outline,
|
|
_ => Icons.chat_bubble_outline,
|
|
};
|
|
}
|
|
|
|
String _formatTime(DateTime time) {
|
|
final now = DateTime.now();
|
|
final diff = now.difference(time);
|
|
if (diff.inMinutes < 60) return '${diff.inMinutes}分钟前';
|
|
if (diff.inHours < 24) return '${diff.inHours}小时前';
|
|
if (diff.inDays < 7) return '${diff.inDays}天前';
|
|
return '${time.month}/${time.day}';
|
|
}
|
|
}
|
|
|
|
(_ColorSet bg, _ColorSet accent) _conversationColors(ActiveAgent agent) {
|
|
return switch (agent) {
|
|
ActiveAgent.health => (const _ColorSet(0xFFE8F5E9), const _ColorSet(0xFF26C281)),
|
|
ActiveAgent.diet => (const _ColorSet(0xFFFFF3E0), const _ColorSet(0xFFFF8C42)),
|
|
ActiveAgent.medication => (const _ColorSet(0xFFFFEBEE), const _ColorSet(0xFFE898A8)),
|
|
ActiveAgent.report => (const _ColorSet(0xFFEDE7F6), const _ColorSet(0xFF8B6CF7)),
|
|
ActiveAgent.exercise => (const _ColorSet(0xFFE0F7FA), const _ColorSet(0xFF00BCD4)),
|
|
ActiveAgent.consultation => (const _ColorSet(0xFFE3F2FD), const _ColorSet(0xFF4D96FF)),
|
|
_ => (const _ColorSet(0xFFF5F5F5), const _ColorSet(0xFF999999)),
|
|
};
|
|
}
|
|
|
|
class _ColorSet extends Color {
|
|
const _ColorSet(int super.value);
|
|
}
|