From e950f290804fecdb2730754399a55c7aeddcb7ef Mon Sep 17 00:00:00 2001 From: Chris Sells Date: Mon, 12 May 2025 14:05:41 -0700 Subject: [PATCH] fix #95: Image Attachment Disappears After Audio Recording --- CHANGELOG.md | 5 +++++ lib/src/views/chat_input/chat_input.dart | 10 ++++++---- lib/src/views/llm_chat_view/llm_chat_view.dart | 13 ++++++++++--- pubspec.yaml | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7b5327..d4c75b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.8.1 + +* fixed [#95](https://github.com/flutter/ai/issues/95): Image Attachment + Disappears After Audio Recording + ## 0.8.0 * fixed [#90](https://github.com/flutter/ai/issues/90): Input box shrinks unexpectedly when clicking file attachment button – customization not diff --git a/lib/src/views/chat_input/chat_input.dart b/lib/src/views/chat_input/chat_input.dart index 2683fb1..ad78529 100644 --- a/lib/src/views/chat_input/chat_input.dart +++ b/lib/src/views/chat_input/chat_input.dart @@ -57,8 +57,10 @@ class ChatInput extends StatefulWidget { /// Callback function triggered when speech-to-text translation is requested. /// - /// Takes an [XFile] representing the audio file to be translated. - final void Function(XFile file) onTranslateStt; + /// Takes an [XFile] representing the audio file to be translated and the + /// current attachments. + final void Function(XFile file, Iterable attachments) + onTranslateStt; /// The initial message to populate the input field, if any. final ChatMessage? initialMessage; @@ -249,8 +251,8 @@ class _ChatInputState extends State { return; } - // will come back as initialMessage - widget.onTranslateStt(file); + // Pass current attachments to onTranslateStt + widget.onTranslateStt(file, List.from(_attachments)); } void onAttachments(Iterable attachments) { diff --git a/lib/src/views/llm_chat_view/llm_chat_view.dart b/lib/src/views/llm_chat_view/llm_chat_view.dart index 2ace111..91f3097 100644 --- a/lib/src/views/llm_chat_view/llm_chat_view.dart +++ b/lib/src/views/llm_chat_view/llm_chat_view.dart @@ -277,7 +277,10 @@ class _LlmChatViewState extends State }); } - Future _onTranslateStt(XFile file) async { + Future _onTranslateStt( + XFile file, + Iterable currentAttachments, + ) async { assert(widget.enableVoiceNotes); _initialMessage = null; _associatedResponse = null; @@ -297,7 +300,9 @@ class _LlmChatViewState extends State attachments: attachments, ), onUpdate: (text) => response += text, - onDone: (error) async => _onSttDone(error, response, file), + onDone: + (error) async => + _onSttDone(error, response, file, currentAttachments), ); setState(() {}); @@ -307,10 +312,12 @@ class _LlmChatViewState extends State LlmException? error, String response, XFile file, + Iterable attachments, ) async { assert(_pendingSttResponse != null); setState(() { - _initialMessage = ChatMessage.user(response, []); + // Preserve any existing attachments from the current input + _initialMessage = ChatMessage.user(response, attachments); _pendingSttResponse = null; }); diff --git a/pubspec.yaml b/pubspec.yaml index c8137d9..8262994 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_ai_toolkit description: >- A set of AI chat-related widgets for Flutter apps targeting mobile, desktop, and web. -version: 0.8.0 +version: 0.8.1 repository: https://github.com/flutter/ai topics: