feat: add senderTitleResolver to override the default sender.firstName for chat messages

This commit is contained in:
Freek van de Ven 2025-02-20 13:34:37 +01:00 committed by FlutterJoey
parent 2508789a6d
commit 590a339d0d
3 changed files with 16 additions and 3 deletions

View file

@ -19,6 +19,7 @@
- Added imagePickerBuilder to the builders in the ChatOptions to override the image picker with a custom implementation that needs to return a Future<Uint8List?> - Added imagePickerBuilder to the builders in the ChatOptions to override the image picker with a custom implementation that needs to return a Future<Uint8List?>
- Changed the ChatBottomInputSection to be multiline and go from 45px to 120px in height depending on how many lines are in the textfield - Changed the ChatBottomInputSection to be multiline and go from 45px to 120px in height depending on how many lines are in the textfield
- Added chatScreenBuilder to the userstory configuration to customize the specific chat screen with a ChatModel as argument - Added chatScreenBuilder to the userstory configuration to customize the specific chat screen with a ChatModel as argument
- Added senderTitleResolver to the ChatOptions to resolve the title of the sender in the chat message
## 4.0.0 ## 4.0.0
- Move to the new user story architecture - Move to the new user story architecture

View file

@ -18,6 +18,7 @@ class ChatOptions {
this.messageTheme, this.messageTheme,
this.messageThemeResolver = _defaultMessageThemeResolver, this.messageThemeResolver = _defaultMessageThemeResolver,
this.chatTitleResolver, this.chatTitleResolver,
this.senderTitleResolver,
this.iconEnabledColor, this.iconEnabledColor,
this.iconDisabledColor, this.iconDisabledColor,
this.chatAlignment, this.chatAlignment,
@ -72,6 +73,11 @@ class ChatOptions {
/// the chat in the ChatDetailScreen. /// the chat in the ChatDetailScreen.
final ChatTitleResolver? chatTitleResolver; final ChatTitleResolver? chatTitleResolver;
/// If [senderTitleResolver] is set, it will be used to get the title of
/// the sender in a chat message. If not set, the [sender.firstName] is used.
/// [sender] can be null if the message is an event.
final SenderTitleResolver? senderTitleResolver;
/// The alignment of the chatmessages in the ChatDetailScreen. /// The alignment of the chatmessages in the ChatDetailScreen.
/// Defaults to [Alignment.bottomCenter] /// Defaults to [Alignment.bottomCenter]
final Alignment? chatAlignment; final Alignment? chatAlignment;
@ -90,6 +96,10 @@ class ChatOptions {
/// a chat. /// a chat.
typedef ChatTitleResolver = String? Function(ChatModel chat); typedef ChatTitleResolver = String? Function(ChatModel chat);
/// Typedef for the senderTitleResolver function that is used to get a title for
/// a sender.
typedef SenderTitleResolver = String? Function(UserModel? user);
/// Typedef for the messageThemeResolver function that is used to get a /// 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 /// [MessageTheme] for a message. This can return null so you can fall back to
/// default values for some messages. /// default values for some messages.

View file

@ -149,8 +149,10 @@ class _ChatMessageBubble extends StatelessWidget {
showFullDate: isNewDate || previousMessage == null, showFullDate: isNewDate || previousMessage == null,
); );
var senderTitle = Text( var senderTitle =
sender?.firstName ?? "", options.senderTitleResolver?.call(sender) ?? sender?.firstName ?? "";
var senderTitleText = Text(
senderTitle,
style: theme.textTheme.titleMedium, style: theme.textTheme.titleMedium,
); );
@ -176,7 +178,7 @@ class _ChatMessageBubble extends StatelessWidget {
children: [ children: [
if (messageTheme.showName! && !isSameSender) ...[ if (messageTheme.showName! && !isSameSender) ...[
SizedBox(height: options.spacing.chatBetweenMessagesPadding), SizedBox(height: options.spacing.chatBetweenMessagesPadding),
senderTitle, senderTitleText,
], ],
const SizedBox(height: 4), const SizedBox(height: 4),
DefaultChatMessageContainer( DefaultChatMessageContainer(