fix: VLM 参数优化 - temperature 0.7, top_p 0.8, 指令放 system+user

- VisionAsync 新增 Temperature=0.7, TopP=0.8
- system prompt 用专业营养识别指令
- userText 用简短"请看图识别食物"配合图片
- 修复重复 prompt 导致 VLM 误读文本的 bug
This commit is contained in:
MingNian
2026-06-03 11:12:06 +08:00
parent c6395ea9b4
commit 78573eaa5f
46 changed files with 955 additions and 801 deletions

View File

@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class MedicationEditPage extends ConsumerStatefulWidget {
final String? medicationId;
const MedicationEditPage({super.key, this.medicationId});
@override ConsumerState<MedicationEditPage> createState() => _MedicationEditPageState();
}
class _MedicationEditPageState extends ConsumerState<MedicationEditPage> {
final _nameCtrl = TextEditingController(text: '阿司匹林肠溶片');
final _dosageCtrl = TextEditingController(text: '100mg');
String _frequency = '每日1次';
String _time = '08:00';
DateTime _startDate = DateTime.now();
String _duration = '长期服用';
@override void dispose() { _nameCtrl.dispose(); _dosageCtrl.dispose(); super.dispose(); }
@override Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
leading: IconButton(icon: const Icon(Icons.chevron_left), onPressed: () => Navigator.pop(context)),
title: const Text('编辑用药', style: TextStyle(color: Color(0xFF1A1A1A), fontWeight: FontWeight.w600)),
centerTitle: true,
actions: [
TextButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('保存成功 ✅'), backgroundColor: Color(0xFF635BFF)));
Navigator.pop(context);
},
child: const Text('保存', style: TextStyle(color: Color(0xFF635BFF), fontWeight: FontWeight.w600)),
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const Text('药品信息', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xFF1A1A1A))),
const SizedBox(height: 12),
TextField(controller: _nameCtrl, decoration: InputDecoration(hintText: '请输入药品名称', filled: true, fillColor: Colors.grey[50], border: OutlineInputBorder(borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none))),
const SizedBox(height: 16),
TextField(controller: _dosageCtrl, decoration: InputDecoration(hintText: '100mg', filled: true, fillColor: Colors.grey[50], border: OutlineInputBorder(borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none))),
const SizedBox(height: 24),
const Text('服用设置', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xFF1A1A1A))),
const SizedBox(height: 12),
GestureDetector(onTap: _pickFrequency, child: Container(padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), decoration: BoxDecoration(border: Border.all(color: const Color(0xFFE0E0E0)), borderRadius: BorderRadius.circular(12)), child: Row(children: [Text(_frequency, style: const TextStyle(fontSize: 15)), const Spacer(), const Icon(Icons.keyboard_arrow_down, size: 20, color: Color(0xFF9E9E9E))]))),
const SizedBox(height: 16),
GestureDetector(onTap: _pickTime, child: Container(padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), decoration: BoxDecoration(border: Border.all(color: const Color(0xFFE0E0E0)), borderRadius: BorderRadius.circular(12)), child: Row(children: [Text(_time, style: const TextStyle(fontSize: 15)), const Spacer(), const Icon(Icons.access_time, size: 20, color: Color(0xFF9E9E9E))]))),
const SizedBox(height: 16),
GestureDetector(onTap: _pickDate, child: Container(padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), decoration: BoxDecoration(border: Border.all(color: const Color(0xFFE0E0E0)), borderRadius: BorderRadius.circular(12)), child: Row(children: [Text('${_startDate.year}-${_startDate.month.toString().padLeft(2, '0')}-${_startDate.day.toString().padLeft(2, '0')}', style: const TextStyle(fontSize: 15)), const Spacer(), const Icon(Icons.calendar_today, size: 20, color: Color(0xFF9E9E9E))]))),
const SizedBox(height: 16),
GestureDetector(onTap: _pickDuration, child: Container(padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), decoration: BoxDecoration(border: Border.all(color: const Color(0xFFE0E0E0)), borderRadius: BorderRadius.circular(12)), child: Row(children: [Text(_duration, style: const TextStyle(fontSize: 15)), const Spacer(), const Icon(Icons.keyboard_arrow_down, size: 20, color: Color(0xFF9E9E9E))]))),
const SizedBox(height: 32),
SizedBox(width: double.infinity, height: 50, child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF635BFF), foregroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25))),
child: const Text('新增用药', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
)),
const SizedBox(height: 20),
]),
),
);
}
void _pickFrequency() async {
final options = ['每日1次', '每日2次', '每日3次', '每周1次', '按需服用'];
final selected = await showModalBottomSheet<String>(
context: context,
builder: (ctx) => SafeArea(child: Column(mainAxisSize: MainAxisSize.min, children: options.map((o) => ListTile(title: Text(o), onTap: () => Navigator.pop(ctx, o))).toList())),
);
if (selected != null && mounted) setState(() => _frequency = selected);
}
void _pickTime() async {
final time = await showTimePicker(context: context, initialTime: TimeOfDay.now());
if (time != null && mounted) setState(() => _time = time.format(context));
}
void _pickDate() async {
final date = await showDatePicker(context: context, firstDate: DateTime(2020), lastDate: DateTime(2030), initialDate: _startDate);
if (date != null && mounted) setState(() => _startDate = date);
}
void _pickDuration() async {
final options = ['长期服用', '7天', '14天', '30天', '90天'];
final selected = await showModalBottomSheet<String>(
context: context,
builder: (ctx) => SafeArea(child: Column(mainAxisSize: MainAxisSize.min, children: options.map((o) => ListTile(title: Text(o), onTap: () => Navigator.pop(ctx, o))).toList())),
);
if (selected != null && mounted) setState(() => _duration = selected);
}
}

View File

@@ -3,78 +3,160 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/navigation_provider.dart';
import '../../providers/data_providers.dart';
/// 用药列表页
class MedicationListPage extends ConsumerWidget {
const MedicationListPage({super.key});
@override Widget build(BuildContext context, WidgetRef ref) {
final meds = ref.watch(medicationListProvider);
return Scaffold(
appBar: AppBar(title: const Text('我的用药')),
body: meds.when(
data: (list) {
if (list.isEmpty) return _empty(context);
return ListView.builder(
itemCount: list.length,
itemBuilder: (ctx, i) {
final m = list[i];
final times = (m['timeOfDay'] as List?)?.cast<String>() ?? [];
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: ListTile(
leading: const Icon(Icons.medication, color: Color(0xFF635BFF)),
title: Text('${m['name']} ${m['dosage'] ?? ''}', style: const TextStyle(fontSize: 16)),
subtitle: Text('每天 ${times.join("")}', style: const TextStyle(fontSize: 14, color: Color(0xFF666666))),
trailing: IconButton(icon: const Icon(Icons.check_circle_outline, color: Color(0xFF43A047)), onPressed: () async {
await ref.read(medicationServiceProvider).confirm(m['id']);
ref.invalidate(medicationListProvider);
}),
),
);
},
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error: (_, _) => _empty(context),
backgroundColor: const Color(0xFFF8F7FF),
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
title: const Text('我的用药', style: TextStyle(color: Color(0xFF1A1A1A), fontWeight: FontWeight.w600)),
centerTitle: true,
actions: [
TextButton(
onPressed: () => pushRoute(ref, 'medicationEdit'),
child: Row(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.add_circle_outline, size: 18, color: Color(0xFF635BFF)),
const SizedBox(width: 4),
const Text('添加新药', style: TextStyle(color: Color(0xFF635BFF), fontSize: 14)),
]),
),
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () { pushRoute(ref, 'medicationAdd'); ref.invalidate(medicationListProvider); },
icon: const Icon(Icons.add), label: const Text('添加药品'),
body: Column(children: [
Container(padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), child: Row(children: [
_TabChip(label: '全部', active: true),
const SizedBox(width: 8),
_TabChip(label: '服用中'),
const SizedBox(width: 8),
_TabChip(label: '已停药'),
])),
Expanded(child: meds.when(
data: (list) {
if (list.isEmpty) return _empty(context);
return ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: list.length + 1,
itemBuilder: (ctx, i) {
if (i == list.length) return const SizedBox(height: 80);
final m = list[i];
return _MedicationCard(data: m);
},
);
},
loading: () => const Center(child: CircularProgressIndicator(color: Color(0xFF635BFF))),
error: (_, __) => _empty(context),
)),
_buildReminderBar(),
]),
);
}
Widget _buildReminderBar() {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(color: Colors.white, boxShadow: [BoxShadow(color: Colors.grey.withAlpha(30), blurRadius: 8)]),
child: Row(children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: const Color(0xFFEDEBFF),
borderRadius: BorderRadius.circular(20),
border: Border.all(color: const Color(0xFF635BFF).withAlpha(50)),
),
child: const Icon(Icons.notifications_active_outlined, size: 20, color: Color(0xFF635BFF)),
),
const SizedBox(width: 12),
const Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('用药提醒已开启', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: Color(0xFF1A1A1A))),
Text('按时服药,守护心脏健康一天', style: TextStyle(fontSize: 12, color: Color(0xFF9E9E9E))),
])),
const Icon(Icons.chevron_right, size: 18, color: Color(0xFFBDBDBD)),
]),
);
}
Widget _empty(BuildContext context) {
return Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.medication_outlined, size: 64, color: Color(0xFFE0E0E0)),
const SizedBox(height: 12),
const Text('暂无用药计划', style: TextStyle(fontSize: 15, color: Color(0xFF9E9E9E))),
const SizedBox(height: 8),
const Text('可通过 AI 对话或手动添加', style: TextStyle(fontSize: 13, color: Color(0xFFBDBDBD))),
]));
}
}
class _TabChip extends StatelessWidget {
final String label;
final bool active;
const _TabChip({required this.label, this.active = false});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 7),
decoration: BoxDecoration(
color: active ? const Color(0xFF635BFF) : Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: active ? const Color(0xFF635BFF) : const Color(0xFFE0E0E0)),
),
child: Text(
label,
style: TextStyle(
fontSize: 13,
fontWeight: active ? FontWeight.w600 : FontWeight.normal,
color: active ? Colors.white : const Color(0xFF757575),
),
),
);
}
Widget _empty(BuildContext context) => Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Icon(Icons.medication, size: 64, color: Colors.grey[300]),
const SizedBox(height: 12), Text('暂无用药计划', style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 8), Text('可通过 AI 对话或手动添加', style: Theme.of(context).textTheme.labelMedium),
]));
}
/// 编辑用药页
class MedicationEditPage extends ConsumerStatefulWidget {
final String? id;
const MedicationEditPage({super.key, this.id});
@override ConsumerState<MedicationEditPage> createState() => _MedicationEditPageState();
}
class _MedicationEditPageState extends ConsumerState<MedicationEditPage> {
final _nameCtrl = TextEditingController(); final _dosageCtrl = TextEditingController(); final _timeCtrl = TextEditingController();
@override void dispose() { _nameCtrl.dispose(); _dosageCtrl.dispose(); _timeCtrl.dispose(); super.dispose(); }
class _MedicationCard extends StatelessWidget {
final Map<String, dynamic> data;
const _MedicationCard({required this.data});
Future<void> _save() async {
await ref.read(medicationServiceProvider).create({
'name': _nameCtrl.text, 'dosage': _dosageCtrl.text,
'frequency': 'Daily', 'timeOfDay': [if (_timeCtrl.text.isNotEmpty) _timeCtrl.text],
'source': 'Manual', 'startDate': DateTime.now().toIso8601String().substring(0, 10),
});
popRoute(ref);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [BoxShadow(color: const Color(0xFF635BFF).withAlpha(10), blurRadius: 4, offset: const Offset(0, 2))],
),
child: Row(children: [
Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: const Color(0xFFF5F3FF),
borderRadius: BorderRadius.circular(14),
),
child: const Center(child: Text('💊', style: TextStyle(fontSize: 24))),
),
const SizedBox(width: 12),
Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('${data['name'] ?? ''}', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xFF1A1A1A))),
const SizedBox(height: 4),
Text('${data['dosage'] ?? ''} · 每日1次', style: const TextStyle(fontSize: 13, color: Color(0xFF9E9E9E))),
const SizedBox(height: 2),
Text('08:00 · 剩余 1 片', style: const TextStyle(fontSize: 12, color: Color(0xFFBDBDBD))),
])),
Container(
width: 28,
height: 28,
decoration: const BoxDecoration(color: Color(0xFFDCFCE7), shape: BoxShape.circle),
child: const Icon(Icons.check, size: 16, color: Color(0xFF43A047)),
),
]),
);
}
@override Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('添加药品')),
body: ListView(padding: const EdgeInsets.all(16), children: [
TextField(controller: _nameCtrl, decoration: const InputDecoration(labelText: '药品名称', hintText: '如:阿司匹林')),
const SizedBox(height: 16), TextField(controller: _dosageCtrl, decoration: const InputDecoration(labelText: '剂量', hintText: '100mg')),
const SizedBox(height: 16), TextField(controller: _timeCtrl, decoration: const InputDecoration(labelText: '服药时间', hintText: '08:00:00')),
const SizedBox(height: 32), SizedBox(width: double.infinity, height: 48, child: ElevatedButton(onPressed: _save, child: const Text('保存'))),
]),
);
}