From 990a89199b343571158d1c266e9cd8fbb3023b0f Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Thu, 13 Feb 2025 15:46:36 +0100 Subject: [PATCH] feat: add chatTitleResolver to ChatOptions to override chatTitle behavior --- packages/flutter_chat/lib/src/config/chat_options.dart | 9 +++++++++ .../src/screens/chat_detail/chat_detail_screen.dart | 10 ++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/flutter_chat/lib/src/config/chat_options.dart b/packages/flutter_chat/lib/src/config/chat_options.dart index d73f5f1..73ac248 100644 --- a/packages/flutter_chat/lib/src/config/chat_options.dart +++ b/packages/flutter_chat/lib/src/config/chat_options.dart @@ -16,6 +16,7 @@ class ChatOptions { this.spacing = const ChatSpacing(), this.messageTheme, this.messageThemeResolver = _defaultMessageThemeResolver, + this.chatTitleResolver, this.iconEnabledColor, this.iconDisabledColor, this.chatAlignment, @@ -64,6 +65,10 @@ class ChatOptions { /// the [messageTheme] will be used. final MessageThemeResolver messageThemeResolver; + /// If [chatTitleResolver] is set, it will be used to get the title of + /// the chat in the ChatDetailScreen. + final ChatTitleResolver? chatTitleResolver; + /// The alignment of the chatmessages in the ChatDetailScreen. /// Defaults to [Alignment.bottomCenter] final Alignment? chatAlignment; @@ -81,6 +86,10 @@ class ChatOptions { final int pageSize; } +/// Typedef for the chatTitleResolver function that is used to get a title for +/// a chat. +typedef ChatTitleResolver = String Function(ChatModel chat); + /// Typedef for the messageThemeResolver function that is used to get a /// [MessageTheme] for a message. This can return null so you can fall back to /// default values for some messages. diff --git a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart index dedf564..0c41d02 100644 --- a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart +++ b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart @@ -21,7 +21,6 @@ class ChatDetailScreen extends HookWidget { required this.onUploadImage, required this.onMessageSubmit, required this.onReadChat, - this.getChatTitle, super.key, }); @@ -44,9 +43,6 @@ class ChatDetailScreen extends HookWidget { /// Callback function triggered when the chat is read. final Function(ChatModel chat) onReadChat; - /// Callback function to get the chat title - final String Function(ChatModel chat)? getChatTitle; - /// Callback for when the user wants to navigate back final VoidCallback? onExit; @@ -74,7 +70,6 @@ class ChatDetailScreen extends HookWidget { _computeChatTitle( chatScope: chatScope, chat: chat, - getChatTitle: getChatTitle, onTitleComputed: (title) => chatTitle.value = title, ), ); @@ -128,11 +123,10 @@ class ChatDetailScreen extends HookWidget { Future _computeChatTitle({ required ChatScope chatScope, required ChatModel chat, - required String? Function(ChatModel chat)? getChatTitle, required void Function(String?) onTitleComputed, }) async { - if (getChatTitle != null) { - onTitleComputed(getChatTitle(chat)); + if (chatScope.options.chatTitleResolver != null) { + onTitleComputed(chatScope.options.chatTitleResolver!.call(chat)); return; }