From 11d8c811613669661b0d4a4452d1c458072d04b0 Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Thu, 20 Feb 2025 13:47:05 +0100 Subject: [PATCH] fix: allow the messageTheme to override default behavior for message time --- .../lib/src/config/chat_options.dart | 18 +++++++++++++++--- .../widgets/default_message_builder.dart | 6 ++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/flutter_chat/lib/src/config/chat_options.dart b/packages/flutter_chat/lib/src/config/chat_options.dart index 21408ae..f1daedd 100644 --- a/packages/flutter_chat/lib/src/config/chat_options.dart +++ b/packages/flutter_chat/lib/src/config/chat_options.dart @@ -124,9 +124,10 @@ class MessageTheme { this.textAlignment, this.showName, this.showTime, + this.showFullDate, }); - /// + /// Creates a [MessageTheme] from a [ThemeData] factory MessageTheme.fromTheme(ThemeData theme) => MessageTheme( backgroundColor: theme.colorScheme.primary, nameColor: theme.colorScheme.onPrimary, @@ -137,8 +138,9 @@ class MessageTheme { textAlignment: TextAlign.start, messageSidePadding: 144.0, messageAlignment: null, - showName: true, + showName: null, showTime: true, + showFullDate: null, ); /// The alignment of the message in the chat @@ -179,13 +181,20 @@ class MessageTheme { final double? messageSidePadding; /// If the name of the sender should be shown above the message - /// Defaults to true + /// If not set the name will be shown if the previous message was not from the + /// same sender. final bool? showName; /// If the time of the message should be shown below the message /// Defaults to true final bool? showTime; + /// If the full date should be shown with the time in the message + /// If not set the date will be shown if the previous message was not on the + /// same day. + /// If [showTime] is false, this value is ignored. + final bool? showFullDate; + /// Creates a copy of the current object with the provided values MessageTheme copyWith({ Color? backgroundColor, @@ -199,6 +208,7 @@ class MessageTheme { TextAlign? textAlignment, bool? showName, bool? showTime, + bool? showFullDate, }) => MessageTheme( backgroundColor: backgroundColor ?? this.backgroundColor, @@ -212,6 +222,7 @@ class MessageTheme { textAlignment: textAlignment ?? this.textAlignment, showName: showName ?? this.showName, showTime: showTime ?? this.showTime, + showFullDate: showFullDate ?? this.showFullDate, ); /// If a value is null in the first object, the value from the second object @@ -228,6 +239,7 @@ class MessageTheme { textAlignment: textAlignment ?? other.textAlignment, showName: showName ?? other.showName, showTime: showTime ?? other.showTime, + showFullDate: showFullDate ?? other.showFullDate, ); } diff --git a/packages/flutter_chat/lib/src/screens/chat_detail/widgets/default_message_builder.dart b/packages/flutter_chat/lib/src/screens/chat_detail/widgets/default_message_builder.dart index e21bd5f..da0576d 100644 --- a/packages/flutter_chat/lib/src/screens/chat_detail/widgets/default_message_builder.dart +++ b/packages/flutter_chat/lib/src/screens/chat_detail/widgets/default_message_builder.dart @@ -144,9 +144,11 @@ class _ChatMessageBubble extends StatelessWidget { var isNewDate = previousMessage != null && message.timestamp.day != previousMessage?.timestamp.day; + var showFullDateOnMessage = + messageTheme.showFullDate ?? (isNewDate || previousMessage == null); var messageTime = dateFormatter.format( date: message.timestamp, - showFullDate: isNewDate || previousMessage == null, + showFullDate: showFullDateOnMessage, ); var senderTitle = @@ -176,7 +178,7 @@ class _ChatMessageBubble extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - if (messageTheme.showName! && !isSameSender) ...[ + if (messageTheme.showName ?? !isSameSender) ...[ SizedBox(height: options.spacing.chatBetweenMessagesPadding), senderTitleText, ],