From 9279b7e28309a1fe151a4812c504a71e134d0f01 Mon Sep 17 00:00:00 2001 From: MingNian <1281442923@qq.com> Date: Tue, 16 Jun 2026 17:08:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A3=9F=E7=89=A9=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=A1=86=E5=88=A0=E9=99=A4bug=20+=20VLM?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=E5=9B=9E=E6=BB=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复饮食识别结果编辑框一次只能删一个字的bug(控制器缓存) - 回滚VLM图片压缩代码到原始状态 - 保持原有显示逻辑不变 --- .../lib/pages/diet/diet_capture_page.dart | 15 +++- .../home/widgets/chat_messages_view.dart | 79 +++++++++---------- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/health_app/lib/pages/diet/diet_capture_page.dart b/health_app/lib/pages/diet/diet_capture_page.dart index d5f57ad..5854579 100644 --- a/health_app/lib/pages/diet/diet_capture_page.dart +++ b/health_app/lib/pages/diet/diet_capture_page.dart @@ -593,6 +593,7 @@ class _DietCapturePageState extends ConsumerState { (v) => ref .read(dietProvider.notifier) .updateFoodName(food.id, v), + fieldKey: '${food.id}_name', style: const TextStyle( fontSize: 17, fontWeight: FontWeight.w600, @@ -608,6 +609,7 @@ class _DietCapturePageState extends ConsumerState { (v) => ref .read(dietProvider.notifier) .updateFoodPortion(food.id, v), + fieldKey: '${food.id}_portion', hint: '份量', style: const TextStyle( fontSize: 14, @@ -629,6 +631,7 @@ class _DietCapturePageState extends ConsumerState { food.id, int.tryParse(v) ?? 0, ), + fieldKey: '${food.id}_cal', hint: '0', style: const TextStyle( fontSize: 14, @@ -664,16 +667,26 @@ class _DietCapturePageState extends ConsumerState { ); } + final Map _fieldCtrls = {}; + + TextEditingController _ctrlFor(String key, String value) { + if (_fieldCtrls.containsKey(key)) return _fieldCtrls[key]!; + final c = TextEditingController(text: value); + _fieldCtrls[key] = c; + return c; + } + Widget _compactField( String value, ValueChanged cb, { + required String fieldKey, String? hint, TextStyle? style, TextAlign align = TextAlign.start, TextInputType? keyboardType, }) { return TextField( - controller: TextEditingController(text: value), + controller: _ctrlFor(fieldKey, value), onChanged: cb, keyboardType: keyboardType, textAlign: align, diff --git a/health_app/lib/pages/home/widgets/chat_messages_view.dart b/health_app/lib/pages/home/widgets/chat_messages_view.dart index fdd4a0a..e6d7a15 100644 --- a/health_app/lib/pages/home/widgets/chat_messages_view.dart +++ b/health_app/lib/pages/home/widgets/chat_messages_view.dart @@ -1245,47 +1245,11 @@ class ChatMessagesView extends ConsumerWidget { ChatMessage msg, WidgetRef ref, ) { - return StatefulBuilder( - builder: (context, setState) { - final controller = TextEditingController(text: field.value); - return TextField( - controller: controller, - autofocus: true, - style: const TextStyle( - fontSize: 18, - fontWeight: FontWeight.w600, - color: AppColors.textPrimary, - ), - decoration: InputDecoration( - contentPadding: const EdgeInsets.symmetric( - horizontal: 12, - vertical: 8, - ), - filled: true, - fillColor: Colors.white, - enabledBorder: OutlineInputBorder( - borderRadius: BorderRadius.circular(8), - borderSide: BorderSide(color: AppColors.primary.withAlpha(80)), - ), - focusedBorder: OutlineInputBorder( - borderRadius: BorderRadius.circular(8), - borderSide: const BorderSide( - color: AppColors.primary, - width: 1.5, - ), - ), - ), - onSubmitted: (value) { - ref - .read(chatProvider.notifier) - .finishEditingField(msg.id, field.label, value); - }, - onTapOutside: (_) { - ref - .read(chatProvider.notifier) - .finishEditingField(msg.id, field.label, controller.text); - }, - ); + return _EditFieldCell( + key: ValueKey('editing_${msg.id}_${field.label}'), + initialValue: field.value, + onDone: (value) { + ref.read(chatProvider.notifier).finishEditingField(msg.id, field.label, value); }, ); } @@ -2478,3 +2442,36 @@ class _ExpandableAdviceState extends State<_ExpandableAdvice> { ); } } + +class _EditFieldCell extends StatefulWidget { + final String initialValue; + final ValueChanged onDone; + const _EditFieldCell({super.key, required this.initialValue, required this.onDone}); + + @override State<_EditFieldCell> createState() => _EditFieldCellState(); +} + +class _EditFieldCellState extends State<_EditFieldCell> { + late final TextEditingController _ctrl; + + @override void initState() { + super.initState(); + _ctrl = TextEditingController(text: widget.initialValue); + } + + @override void dispose() { _ctrl.dispose(); super.dispose(); } + + @override Widget build(BuildContext context) => TextField( + controller: _ctrl, + autofocus: true, + style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600, color: Color(0xFF1E293B)), + decoration: InputDecoration( + contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), + filled: true, fillColor: Colors.white, + enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: const Color(0xFF6366F1).withAlpha(80))), + focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Color(0xFF6366F1), width: 1.5)), + ), + onSubmitted: widget.onDone, + onTapOutside: (_) => widget.onDone(_ctrl.text), + ); +}