Skip to content

Commit 1c9f078

Browse files
authored
feat(ui): Add support for gradient backgrounds in MessageCard (#2383)
1 parent 3226700 commit 1c9f078

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

packages/stream_chat_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Upcoming
22

3+
✅ Added
4+
5+
- Added `messageBackgroundGradient` property to `StreamMessageThemeData` for gradient message
6+
backgrounds.
7+
38
🐞 Fixed
49

510
- Fixed `.replaceMentions` not escaping special characters in the username.

packages/stream_chat_flutter/lib/src/message_widget/message_card.dart

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,7 @@ class _MessageCardState extends State<MessageCard> {
169169
(widget.showUserAvatar == DisplayWidget.gone ? 0 : 4.0),
170170
),
171171
clipBehavior: Clip.hardEdge,
172-
decoration: ShapeDecoration(
173-
color: _getBackgroundColor(),
174-
shape: widget.shape ??
175-
RoundedRectangleBorder(
176-
side: widget.borderSide ??
177-
BorderSide(
178-
color: widget.messageTheme.messageBorderColor ??
179-
Colors.transparent,
180-
),
181-
borderRadius: widget.borderRadiusGeometry ?? BorderRadius.zero,
182-
),
183-
),
172+
decoration: _buildDecoration(widget.messageTheme),
184173
child: Column(
185174
mainAxisSize: MainAxisSize.min,
186175
crossAxisAlignment: CrossAxisAlignment.start,
@@ -234,22 +223,49 @@ class _MessageCardState extends State<MessageCard> {
234223
);
235224
}
236225

237-
Color? _getBackgroundColor() {
226+
ShapeDecoration _buildDecoration(StreamMessageThemeData theme) {
227+
final gradient = _getBackgroundGradient(theme);
228+
final color = gradient == null ? _getBackgroundColor(theme) : null;
229+
230+
final borderColor = theme.messageBorderColor ?? Colors.transparent;
231+
final borderRadius = widget.borderRadiusGeometry ?? BorderRadius.zero;
232+
233+
return ShapeDecoration(
234+
color: color,
235+
gradient: gradient,
236+
shape: switch (widget.shape) {
237+
final shape? => shape,
238+
_ => RoundedRectangleBorder(
239+
borderRadius: borderRadius,
240+
side: switch (widget.borderSide) {
241+
final side? => side,
242+
_ => BorderSide(color: borderColor),
243+
},
244+
),
245+
},
246+
);
247+
}
248+
249+
Color? _getBackgroundColor(StreamMessageThemeData theme) {
238250
if (widget.hasQuotedMessage) {
239-
return widget.messageTheme.messageBackgroundColor;
251+
return theme.messageBackgroundColor;
240252
}
241253

242254
final containsOnlyUrlAttachment =
243255
widget.hasUrlAttachments && !widget.hasNonUrlAttachments;
244256

245257
if (containsOnlyUrlAttachment) {
246-
return widget.messageTheme.urlAttachmentBackgroundColor;
258+
return theme.urlAttachmentBackgroundColor;
247259
}
248260

249-
if (widget.isOnlyEmoji) {
250-
return Colors.transparent;
251-
}
261+
if (widget.isOnlyEmoji) return null;
262+
263+
return theme.messageBackgroundColor;
264+
}
265+
266+
Gradient? _getBackgroundGradient(StreamMessageThemeData theme) {
267+
if (widget.isOnlyEmoji) return null;
252268

253-
return widget.messageTheme.messageBackgroundColor;
269+
return theme.messageBackgroundGradient;
254270
}
255271
}

packages/stream_chat_flutter/lib/src/theme/message_theme.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class StreamMessageThemeData with Diagnosticable {
1717
this.messageLinksStyle,
1818
this.messageDeletedStyle,
1919
this.messageBackgroundColor,
20+
this.messageBackgroundGradient,
2021
this.messageBorderColor,
2122
this.reactionsBackgroundColor,
2223
this.reactionsBorderColor,
@@ -54,6 +55,11 @@ class StreamMessageThemeData with Diagnosticable {
5455
/// Color for messageBackgroundColor
5556
final Color? messageBackgroundColor;
5657

58+
/// Gradient for message background.
59+
///
60+
/// Note: If this is set, it will override [messageBackgroundColor].
61+
final Gradient? messageBackgroundGradient;
62+
5763
/// Color for message border color
5864
final Color? messageBorderColor;
5965

@@ -96,6 +102,7 @@ class StreamMessageThemeData with Diagnosticable {
96102
TextStyle? createdAtStyle,
97103
TextStyle? repliesStyle,
98104
Color? messageBackgroundColor,
105+
Gradient? messageBackgroundGradient,
99106
Color? messageBorderColor,
100107
StreamAvatarThemeData? avatarTheme,
101108
Color? reactionsBackgroundColor,
@@ -116,6 +123,8 @@ class StreamMessageThemeData with Diagnosticable {
116123
messageDeletedStyle: messageDeletedStyle ?? this.messageDeletedStyle,
117124
messageBackgroundColor:
118125
messageBackgroundColor ?? this.messageBackgroundColor,
126+
messageBackgroundGradient:
127+
messageBackgroundGradient ?? this.messageBackgroundGradient,
119128
messageBorderColor: messageBorderColor ?? this.messageBorderColor,
120129
avatarTheme: avatarTheme ?? this.avatarTheme,
121130
repliesStyle: repliesStyle ?? this.repliesStyle,
@@ -154,6 +163,8 @@ class StreamMessageThemeData with Diagnosticable {
154163
TextStyle.lerp(a.messageDeletedStyle, b.messageDeletedStyle, t),
155164
messageBackgroundColor:
156165
Color.lerp(a.messageBackgroundColor, b.messageBackgroundColor, t),
166+
messageBackgroundGradient:
167+
t < 0.5 ? a.messageBackgroundGradient : b.messageBackgroundGradient,
157168
messageBorderColor:
158169
Color.lerp(a.messageBorderColor, b.messageBorderColor, t),
159170
messageLinksStyle:
@@ -218,6 +229,7 @@ class StreamMessageThemeData with Diagnosticable {
218229
repliesStyle:
219230
repliesStyle?.merge(other.repliesStyle) ?? other.repliesStyle,
220231
messageBackgroundColor: other.messageBackgroundColor,
232+
messageBackgroundGradient: other.messageBackgroundGradient,
221233
messageBorderColor: other.messageBorderColor,
222234
avatarTheme: avatarTheme?.merge(other.avatarTheme) ?? other.avatarTheme,
223235
reactionsBackgroundColor: other.reactionsBackgroundColor,
@@ -244,6 +256,7 @@ class StreamMessageThemeData with Diagnosticable {
244256
messageDeletedStyle == other.messageDeletedStyle &&
245257
repliesStyle == other.repliesStyle &&
246258
messageBackgroundColor == other.messageBackgroundColor &&
259+
messageBackgroundGradient == other.messageBackgroundGradient &&
247260
messageBorderColor == other.messageBorderColor &&
248261
reactionsBackgroundColor == other.reactionsBackgroundColor &&
249262
reactionsBorderColor == other.reactionsBorderColor &&
@@ -265,6 +278,7 @@ class StreamMessageThemeData with Diagnosticable {
265278
messageDeletedStyle.hashCode ^
266279
repliesStyle.hashCode ^
267280
messageBackgroundColor.hashCode ^
281+
messageBackgroundGradient.hashCode ^
268282
messageBorderColor.hashCode ^
269283
reactionsBackgroundColor.hashCode ^
270284
reactionsBorderColor.hashCode ^
@@ -288,6 +302,8 @@ class StreamMessageThemeData with Diagnosticable {
288302
..add(DiagnosticsProperty('messageDeletedStyle', messageDeletedStyle))
289303
..add(DiagnosticsProperty('repliesStyle', repliesStyle))
290304
..add(ColorProperty('messageBackgroundColor', messageBackgroundColor))
305+
..add(DiagnosticsProperty(
306+
'messageBackgroundGradient', messageBackgroundGradient))
291307
..add(ColorProperty('messageBorderColor', messageBorderColor))
292308
..add(DiagnosticsProperty('avatarTheme', avatarTheme))
293309
..add(ColorProperty('reactionsBackgroundColor', reactionsBackgroundColor))

0 commit comments

Comments
 (0)