From 02ae851d134b3bcaf845d6e237af8874987f2f3f Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Mon, 17 Feb 2025 15:14:39 +0100 Subject: [PATCH] feat: submit the chat message inputfield when enter is pressed --- CHANGELOG.md | 1 + packages/flutter_chat/lib/src/config/chat_builders.dart | 1 + .../lib/src/screens/chat_detail/widgets/chat_bottom.dart | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f8764..962e0d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Added loadNewMessagesAfter, loadOldMessagesBefore and removed pagination from getMessages in the ChatRepositoryInterface to change pagination behavior to rely on the stream and two methods indicating that more messages should be added to the stream - Added chatTitleResolver that can be used to resolve the chat title from the chat model or return null to allow for default behavior - Added ChatPaginationControls to the ChatOptions to allow for more control over the pagination +- Fixed that chat message is automatically sent when the user presses enter on the keyboard in the chat input ## 4.0.0 - Move to the new user story architecture diff --git a/packages/flutter_chat/lib/src/config/chat_builders.dart b/packages/flutter_chat/lib/src/config/chat_builders.dart index 26a2322..9da4148 100644 --- a/packages/flutter_chat/lib/src/config/chat_builders.dart +++ b/packages/flutter_chat/lib/src/config/chat_builders.dart @@ -106,6 +106,7 @@ typedef TextInputBuilder = Widget Function( TextEditingController textEditingController, Widget suffixIcon, ChatTranslations translations, + VoidCallback onSubmit, ); /// The base screen builder diff --git a/packages/flutter_chat/lib/src/screens/chat_detail/widgets/chat_bottom.dart b/packages/flutter_chat/lib/src/screens/chat_detail/widgets/chat_bottom.dart index 51adf2f..b25b2fd 100644 --- a/packages/flutter_chat/lib/src/screens/chat_detail/widgets/chat_bottom.dart +++ b/packages/flutter_chat/lib/src/screens/chat_detail/widgets/chat_bottom.dart @@ -77,6 +77,8 @@ class ChatBottomInputSection extends HookWidget { ], ); + Future onSubmitField() async => sendMessage(); + var defaultInputField = TextField( textAlign: TextAlign.start, textAlignVertical: TextAlignVertical.center, @@ -106,6 +108,7 @@ class ChatBottomInputSection extends HookWidget { ), suffixIcon: messageSendButtons, ), + onSubmitted: (_) async => onSubmitField(), ); return Padding( @@ -117,6 +120,7 @@ class ChatBottomInputSection extends HookWidget { textController, messageSendButtons, options.translations, + onSubmitField, ) ?? defaultInputField, ),