forked from flyerhq/flutter_chat_ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_message.dart
315 lines (275 loc) · 10.2 KB
/
text_message.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import 'package:flutter/material.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:flutter_link_previewer/flutter_link_previewer.dart'
show LinkPreview, regexLink;
import 'package:flutter_parsed_text/flutter_parsed_text.dart';
import '../../models/emoji_enlargement_behavior.dart';
import '../../models/matchers.dart';
import '../../models/pattern_style.dart';
import '../../util.dart';
import '../state/inherited_chat_theme.dart';
import '../state/inherited_user.dart';
import 'user_name.dart';
/// A class that represents text message widget with optional link preview.
class TextMessage extends StatelessWidget {
/// Creates a text message widget from a [types.TextMessage] class.
const TextMessage({
super.key,
required this.emojiEnlargementBehavior,
required this.hideBackgroundOnEmojiMessages,
required this.message,
this.nameBuilder,
this.onPreviewDataFetched,
this.options = const TextMessageOptions(),
required this.showName,
required this.usePreviewData,
this.userAgent,
});
/// See [Message.emojiEnlargementBehavior].
final EmojiEnlargementBehavior emojiEnlargementBehavior;
/// See [Message.hideBackgroundOnEmojiMessages].
final bool hideBackgroundOnEmojiMessages;
/// [types.TextMessage].
final types.TextMessage message;
/// This is to allow custom user name builder
/// By using this we can fetch newest user info based on id.
final Widget Function(types.User)? nameBuilder;
/// See [LinkPreview.onPreviewDataFetched].
final void Function(types.TextMessage, types.PreviewData)?
onPreviewDataFetched;
/// Customisation options for the [TextMessage].
final TextMessageOptions options;
/// Show user name for the received message. Useful for a group chat.
final bool showName;
/// Enables link (URL) preview.
final bool usePreviewData;
/// User agent to fetch preview data with.
final String? userAgent;
Widget _linkPreview(
types.User user,
double width,
BuildContext context,
) {
final linkDescriptionTextStyle = user.id == message.author.id
? InheritedChatTheme.of(context)
.theme
.sentMessageLinkDescriptionTextStyle
: InheritedChatTheme.of(context)
.theme
.receivedMessageLinkDescriptionTextStyle;
final linkTitleTextStyle = user.id == message.author.id
? InheritedChatTheme.of(context).theme.sentMessageLinkTitleTextStyle
: InheritedChatTheme.of(context)
.theme
.receivedMessageLinkTitleTextStyle;
return LinkPreview(
enableAnimation: true,
metadataTextStyle: linkDescriptionTextStyle,
metadataTitleStyle: linkTitleTextStyle,
onLinkPressed: options.onLinkPressed,
onPreviewDataFetched: _onPreviewDataFetched,
openOnPreviewImageTap: options.openOnPreviewImageTap,
openOnPreviewTitleTap: options.openOnPreviewTitleTap,
padding: EdgeInsets.symmetric(
horizontal:
InheritedChatTheme.of(context).theme.messageInsetsHorizontal,
vertical: InheritedChatTheme.of(context).theme.messageInsetsVertical,
),
previewData: message.previewData,
text: message.text,
textWidget: _textWidgetBuilder(user, context, false),
imageBuilder: options.previewImageBuilder != null
? (url) => options.previewImageBuilder!.call(context, url)
: null,
userAgent: userAgent,
width: width,
);
}
void _onPreviewDataFetched(types.PreviewData previewData) {
if (message.previewData == null) {
onPreviewDataFetched?.call(message, previewData);
}
}
Widget _textWidgetBuilder(
types.User user,
BuildContext context,
bool enlargeEmojis,
) {
final theme = InheritedChatTheme.of(context).theme;
final bodyLinkTextStyle = user.id == message.author.id
? InheritedChatTheme.of(context).theme.sentMessageBodyLinkTextStyle
: InheritedChatTheme.of(context).theme.receivedMessageBodyLinkTextStyle;
final bodyTextStyle = user.id == message.author.id
? theme.sentMessageBodyTextStyle
: theme.receivedMessageBodyTextStyle;
final boldTextStyle = user.id == message.author.id
? theme.sentMessageBodyBoldTextStyle
: theme.receivedMessageBodyBoldTextStyle;
final codeTextStyle = user.id == message.author.id
? theme.sentMessageBodyCodeTextStyle
: theme.receivedMessageBodyCodeTextStyle;
final emojiTextStyle = user.id == message.author.id
? theme.sentEmojiMessageTextStyle
: theme.receivedEmojiMessageTextStyle;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (showName)
nameBuilder?.call(message.author) ?? UserName(author: message.author),
if (enlargeEmojis)
if (options.isTextSelectable)
SelectableText(message.text, style: emojiTextStyle)
else
Text(message.text, style: emojiTextStyle)
else
options.textMessageTextBuilder != null
? options.textMessageTextBuilder!(
context,
message.text,
options,
bodyLinkTextStyle: bodyLinkTextStyle,
bodyTextStyle: bodyTextStyle,
boldTextStyle: boldTextStyle,
codeTextStyle: codeTextStyle,
)
: TextMessageText(
bodyLinkTextStyle: bodyLinkTextStyle,
bodyTextStyle: bodyTextStyle,
boldTextStyle: boldTextStyle,
codeTextStyle: codeTextStyle,
options: options,
text: message.text,
),
],
);
}
@override
Widget build(BuildContext context) {
final enlargeEmojis =
emojiEnlargementBehavior != EmojiEnlargementBehavior.never &&
isConsistsOfEmojis(emojiEnlargementBehavior, message);
final theme = InheritedChatTheme.of(context).theme;
final user = InheritedUser.of(context).user;
final width = MediaQuery.of(context).size.width;
if (usePreviewData && onPreviewDataFetched != null) {
final urlRegexp = RegExp(regexLink, caseSensitive: false);
final matches = urlRegexp.allMatches(message.text);
if (matches.isNotEmpty) {
return _linkPreview(user, width, context);
}
}
return Container(
margin: EdgeInsets.symmetric(
horizontal: theme.messageInsetsHorizontal,
vertical: theme.messageInsetsVertical,
),
child: _textWidgetBuilder(user, context, enlargeEmojis),
);
}
}
/// Widget to reuse the markdown capabilities, e.g., for previews.
class TextMessageText extends StatelessWidget {
const TextMessageText({
super.key,
this.bodyLinkTextStyle,
required this.bodyTextStyle,
this.boldTextStyle,
this.codeTextStyle,
this.maxLines,
this.options = const TextMessageOptions(),
this.overflow = TextOverflow.clip,
required this.text,
});
/// Style to apply to anything that matches a link.
final TextStyle? bodyLinkTextStyle;
/// Regular style to use for any unmatched text. Also used as basis for the fallback options.
final TextStyle bodyTextStyle;
/// Style to apply to anything that matches bold markdown.
final TextStyle? boldTextStyle;
/// Style to apply to anything that matches code markdown.
final TextStyle? codeTextStyle;
/// See [ParsedText.maxLines].
final int? maxLines;
/// See [TextMessage.options].
final TextMessageOptions options;
/// See [ParsedText.overflow].
final TextOverflow overflow;
/// Text that is shown as markdown.
final String text;
@override
Widget build(BuildContext context) => ParsedText(
parse: [
...options.matchers,
mailToMatcher(
style: bodyLinkTextStyle ??
bodyTextStyle.copyWith(
decoration: TextDecoration.underline,
),
),
urlMatcher(
onLinkPressed: options.onLinkPressed,
style: bodyLinkTextStyle ??
bodyTextStyle.copyWith(
decoration: TextDecoration.underline,
),
),
boldMatcher(
style: boldTextStyle ??
bodyTextStyle.merge(PatternStyle.bold.textStyle),
),
italicMatcher(
style: bodyTextStyle.merge(PatternStyle.italic.textStyle),
),
lineThroughMatcher(
style: bodyTextStyle.merge(PatternStyle.lineThrough.textStyle),
),
codeMatcher(
style: codeTextStyle ??
bodyTextStyle.merge(PatternStyle.code.textStyle),
),
],
maxLines: maxLines,
overflow: overflow,
regexOptions: const RegexOptions(multiLine: true, dotAll: true),
selectable: options.isTextSelectable,
style: bodyTextStyle,
text: text,
textWidthBasis: TextWidthBasis.longestLine,
);
}
@immutable
class TextMessageOptions {
const TextMessageOptions({
this.isTextSelectable = true,
this.onLinkPressed,
this.openOnPreviewImageTap = false,
this.openOnPreviewTitleTap = false,
this.matchers = const [],
this.previewImageBuilder,
this.textMessageTextBuilder,
});
/// Whether user can tap and hold to select a text content.
final bool isTextSelectable;
/// Custom link press handler.
final void Function(String)? onLinkPressed;
/// See [LinkPreview.openOnPreviewImageTap].
final bool openOnPreviewImageTap;
/// See [LinkPreview.openOnPreviewTitleTap].
final bool openOnPreviewTitleTap;
/// Additional matchers to parse the text.
final List<MatchText> matchers;
/// Custom link preview imageBuilder.
final PreviewImageBuilder? previewImageBuilder;
/// Add TextMessageTextBuilder to build custom text example markdown, code highlight, etc.
final TextMessageTextBuilder? textMessageTextBuilder;
}
typedef PreviewImageBuilder = Widget Function(BuildContext context, String url);
typedef TextMessageTextBuilder = Widget Function(
BuildContext context,
String text,
TextMessageOptions options, {
TextStyle? bodyLinkTextStyle,
TextStyle? bodyTextStyle,
TextStyle? boldTextStyle,
TextStyle? codeTextStyle,
});