feat: add chatTitleResolver to ChatOptions to override chatTitle behavior

This commit is contained in:
Freek van de Ven 2025-02-13 15:46:36 +01:00
parent ab8045e6bf
commit cb1ceb456a
2 changed files with 11 additions and 8 deletions

View file

@ -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.

View file

@ -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<void> _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;
}