refactor: 死代码清理 + 趋势图重构 + 侧边栏交互优化 + 智能体卡片去延迟

- 死代码清理: 删除 AppCard/AppMenuItem/AppTabChip/AppButtons 等 4 个未使用 Widget 文件; 清理 common_widgets/enterprise_widgets/app_status_badge 中未使用 class; 移除 HealthService 等未使用方法; 后端移除 ExerciseService.CreateFromItemsAsync/HealthArchiveService.GetOrCreateAsync/MedicationAgentHandler 死分支/ChatRequest DTO
- 趋势图: 直线折线 + 渐变填充 + 阴影; 去网格; X 轴日+月份; Y 轴整数刻度最多 4 个; 点击数据点/录入记录显示上方浮层; 选中点变实心
- 侧边栏: 对话记录改单选删除(灰底高亮); 操作栏浮层修复触摸问题; 常用功能/健康仪表盘间距收紧; _Panel 去外框
- 智能体: 欢迎卡片去掉 400ms 延迟; 胶囊去阴影
- 其他: api_client IP 适配; 多页面 UI 微调; 新增 chat_provider/prelaunch_guardrails 测试
This commit is contained in:
MingNian
2026-07-09 15:04:02 +08:00
parent 1c020b8ae5
commit d82e006cf4
37 changed files with 1842 additions and 2096 deletions

View File

@@ -21,31 +21,100 @@ class HealthDrawer extends ConsumerWidget {
return DrawerShell(
widthFactor: 0.9,
child: SafeArea(
child: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: [
SliverPadding(
padding: const EdgeInsets.fromLTRB(12, 16, 12, 24),
sliver: SliverList.list(
showBackground: false,
child: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: [
SliverToBoxAdapter(
child: _DrawerHero(
user: auth.user,
latestHealth: latestHealth,
ref: ref,
),
),
SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.fromLTRB(14, 10, 14, 18),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(28)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_AccountHeader(user: auth.user, ref: ref),
const SizedBox(height: 18),
_HealthDashboard(latestHealth: latestHealth, ref: ref),
const SizedBox(height: 18),
_NavigationSection(ref: ref),
const SizedBox(height: 18),
const SizedBox(height: 14),
const Divider(
height: 1,
thickness: 0.7,
color: Color(0xFFE8ECF2),
),
const SizedBox(height: 14),
_HistorySection(ref: ref),
],
),
),
],
),
),
],
),
);
}
}
class _DrawerHero extends StatelessWidget {
final dynamic user;
final AsyncValue<Map<String, dynamic>> latestHealth;
final WidgetRef ref;
const _DrawerHero({
required this.user,
required this.latestHealth,
required this.ref,
});
@override
Widget build(BuildContext context) {
final topPadding = MediaQuery.paddingOf(context).top;
return Stack(
children: [
Positioned.fill(
child: Image.asset(
'assets/branding/drawer_background_v1.png',
fit: BoxFit.cover,
alignment: Alignment.topLeft,
),
),
Positioned.fill(
child: DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.white.withValues(alpha: 0.12),
Colors.white.withValues(alpha: 0.35),
Colors.white.withValues(alpha: 0.82),
],
stops: const [0.0, 0.58, 1.0],
),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(16, topPadding + 16, 16, 12),
child: Column(
children: [
_AccountHeader(user: user, ref: ref),
const SizedBox(height: 16),
_HealthDashboard(latestHealth: latestHealth, ref: ref),
],
),
),
],
);
}
}
class _AccountHeader extends StatelessWidget {
final dynamic user;
final WidgetRef ref;
@@ -360,13 +429,14 @@ class _NavigationSection extends StatelessWidget {
return _LightSection(
title: '常用功能',
child: GridView.builder(
padding: EdgeInsets.zero,
itemCount: items.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisExtent: 48,
mainAxisSpacing: 8,
crossAxisCount: 4,
mainAxisExtent: 82,
mainAxisSpacing: 7,
crossAxisSpacing: 8,
),
itemBuilder: (context, index) {
@@ -412,38 +482,42 @@ class _NavTile extends StatelessWidget {
return InkWell(
onTap: onTap,
borderRadius: AppRadius.mdBorder,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.54),
borderRadius: AppRadius.mdBorder,
),
child: Row(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 30,
height: 30,
width: 52,
height: 52,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: _colors,
),
borderRadius: AppRadius.smBorder,
borderRadius: AppRadius.mdBorder,
boxShadow: [
BoxShadow(
color: _colors.last.withValues(alpha: 0.18),
blurRadius: 10,
offset: const Offset(0, 5),
),
],
),
child: Icon(item.icon, color: Colors.white, size: 19),
child: Icon(item.icon, color: Colors.white, size: 26),
),
const SizedBox(width: 8),
Expanded(
child: Text(
_title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w800,
color: AppColors.textPrimary,
),
const SizedBox(height: 6),
Text(
_title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: AppColors.textPrimary,
height: 1.1,
),
),
],
@@ -471,8 +545,8 @@ class _LightSection extends StatelessWidget {
child: Text(
title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w900,
fontSize: 18,
fontWeight: FontWeight.w800,
color: AppColors.textPrimary,
),
),
@@ -480,7 +554,7 @@ class _LightSection extends StatelessWidget {
],
),
),
const SizedBox(height: 10),
const SizedBox(height: 2),
child,
],
);
@@ -516,10 +590,6 @@ class _Panel extends StatelessWidget {
],
),
borderRadius: AppRadius.xlBorder,
border: Border.all(
color: Colors.white.withValues(alpha: 0.82),
width: 1.2,
),
boxShadow: AppShadows.soft,
),
child: Column(
@@ -532,7 +602,7 @@ class _Panel extends StatelessWidget {
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w900,
fontWeight: FontWeight.w800,
color: titleColor,
),
),
@@ -636,24 +706,53 @@ class _NavItem {
required this.colors,
});
}
class _HistorySection extends ConsumerWidget {
class _HistorySection extends ConsumerStatefulWidget {
final WidgetRef ref;
const _HistorySection({required this.ref});
static const int _previewCount = 5;
@override
Widget build(BuildContext context, WidgetRef _) {
ConsumerState<_HistorySection> createState() => _HistorySectionState();
}
class _HistorySectionState extends ConsumerState<_HistorySection> {
String? _selectedId;
bool _selecting = false;
bool _deleting = false;
void _enterSelect(String id) {
setState(() {
_selecting = true;
_selectedId = id;
});
}
void _toggleSelect(String id) {
setState(() {
if (_selectedId == id) {
_selectedId = null;
_selecting = false;
} else {
_selectedId = id;
}
});
}
void _exitSelect() {
setState(() {
_selecting = false;
_selectedId = null;
});
}
@override
Widget build(BuildContext context) {
final async = ref.watch(conversationHistoryProvider);
return _Panel(
return _LightSection(
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),
@@ -682,204 +781,268 @@ class _HistorySection extends ConsumerWidget {
),
);
}
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,
final preview = list.take(_HistorySection._previewCount).toList();
return ConstrainedBox(
constraints: const BoxConstraints(minHeight: 48),
child: Stack(
clipBehavior: Clip.none,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (var i = 0; i < preview.length; i++)
_DrawerHistoryTile(
item: preview[i],
isLast: i == preview.length - 1,
selecting: _selecting,
selected: _selectedId == preview[i].id,
onTap: () async {
if (_selecting) {
_toggleSelect(preview[i].id);
return;
}
final error = await ref
.read(chatProvider.notifier)
.loadConversation(preview[i].id);
if (!context.mounted) return;
if (error != null) {
AppToast.show(
context,
error,
type: AppToastType.error,
);
return;
}
Navigator.of(context).maybePop();
},
onLongPress: () => _enterSelect(preview[i].id),
),
if (list.length > _HistorySection._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,
),
),
),
),
),
],
],
),
if (_selecting && _selectedId != null)
Positioned(
top: -46,
right: 2,
child: _HistorySelectionBar(
deleting: _deleting,
onCancel: _exitSelect,
onDelete: () => _deleteSelected(context),
),
),
),
],
],
),
);
},
),
);
}
Future<void> _deleteSelected(BuildContext context) async {
final id = _selectedId;
if (id == null || _deleting) return;
setState(() => _deleting = true);
try {
await ref.read(conversationHistoryProvider.notifier).deleteOne(id);
_exitSelect();
} catch (_) {
if (context.mounted) {
AppToast.show(context, '删除失败,请稍后重试', type: AppToastType.error);
}
} finally {
if (mounted) setState(() => _deleting = false);
}
}
}
class _HistoryActions extends StatelessWidget {
final WidgetRef ref;
final AsyncValue<List<ConversationListItem>> async;
const _HistoryActions({required this.ref, required this.async});
class _HistorySelectionBar extends StatelessWidget {
final bool deleting;
final VoidCallback onCancel;
final VoidCallback onDelete;
const _HistorySelectionBar({
required this.deleting,
required this.onCancel,
required this.onDelete,
});
@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),
),
),
return Align(
alignment: Alignment.centerRight,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: const Color(0xFFE5E7EB)),
boxShadow: AppColors.cardShadowLight,
),
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,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
_HistoryActionButton(
icon: Icons.delete_outline_rounded,
label: deleting ? '删除中' : '删除',
color: AppColors.error,
onTap: deleting ? null : onDelete,
),
),
],
);
}
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('清空'),
),
],
const SizedBox(width: 8),
Container(width: 1, height: 22, color: const Color(0xFFE5E7EB)),
const SizedBox(width: 8),
_HistoryActionButton(
icon: Icons.close_rounded,
label: '取消',
color: AppColors.textSecondary,
onTap: onCancel,
),
],
),
),
);
}
}
class _HistoryActionButton extends StatelessWidget {
final IconData icon;
final String label;
final Color color;
final VoidCallback? onTap;
const _HistoryActionButton({
required this.icon,
required this.label,
required this.color,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
icon,
size: 18,
color: onTap == null ? AppColors.textHint : color,
),
const SizedBox(width: 5),
Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: onTap == null ? AppColors.textHint : color,
),
),
],
),
),
);
if (ok != true) return;
try {
await ref.read(conversationHistoryProvider.notifier).clearAll();
} catch (_) {
if (context.mounted) {
AppToast.show(
context,
'清空失败,请稍后重试',
type: AppToastType.error,
);
}
}
}
}
class _DrawerHistoryTile extends StatelessWidget {
final ConversationListItem item;
final bool isLast;
final bool selecting;
final bool selected;
final VoidCallback onTap;
final VoidCallback onDelete;
final VoidCallback onLongPress;
const _DrawerHistoryTile({
required this.item,
required this.isLast,
required this.selecting,
required this.selected,
required this.onTap,
required this.onDelete,
required this.onLongPress,
});
@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,
return InkWell(
onTap: onTap,
onLongPress: onLongPress,
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 2),
decoration: selected
? BoxDecoration(
color: const Color(0xFFF1F3F7),
borderRadius: BorderRadius.circular(8),
)
: null,
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 11),
child: Row(
children: [
Expanded(
child: Text(
_displaySummary(item),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w800,
color: selected
? AppColors.textSecondary
: AppColors.textPrimary,
height: 1.2,
),
),
),
),
const SizedBox(width: 10),
Text(
_shortDate(item.updatedAt),
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: AppColors.textHint,
),
),
],
),
const SizedBox(width: 10),
Text(
_shortDate(item.updatedAt),
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: AppColors.textHint,
),
),
if (!isLast)
const Divider(
height: 1,
thickness: 0.7,
color: Color(0xFFE8ECF2),
),
],
),
],
),
),
);