forked from flyerhq/flutter_chat_ui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
text_message.dart
165 lines (148 loc) · 5.54 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
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 '../models/emoji_enlargement_behavior.dart';
import '../util.dart';
import 'inherited_chat_theme.dart';
import 'inherited_user.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({
Key? key,
required this.emojiEnlargementBehavior,
required this.hideBackgroundOnEmojiMessages,
required this.message,
this.onPreviewDataFetched,
required this.usePreviewData,
required this.showName,
}) : super(key: key);
/// See [Message.emojiEnlargementBehavior]
final EmojiEnlargementBehavior emojiEnlargementBehavior;
/// See [Message.hideBackgroundOnEmojiMessages]
final bool hideBackgroundOnEmojiMessages;
/// [types.TextMessage]
final types.TextMessage message;
/// See [LinkPreview.onPreviewDataFetched]
final void Function(types.TextMessage, types.PreviewData)?
onPreviewDataFetched;
/// Show user name for the received message. Useful for a group chat.
final bool showName;
/// Enables link (URL) preview
final bool usePreviewData;
void _onPreviewDataFetched(types.PreviewData previewData) {
if (message.previewData == null) {
onPreviewDataFetched?.call(message, previewData);
}
}
Widget _linkPreview(
types.User user,
double width,
BuildContext context,
) {
final bodyTextStyle = user.id == message.author.id
? InheritedChatTheme.of(context).theme.sentMessageBodyTextStyle
: InheritedChatTheme.of(context).theme.receivedMessageBodyTextStyle;
final bodyLinkTextStyle = user.id == message.author.id
? InheritedChatTheme.of(context).theme.sentMessageBodyLinkTextStyle
: InheritedChatTheme.of(context).theme.receivedMessageBodyLinkTextStyle;
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;
final color = getUserAvatarNameColor(message.author,
InheritedChatTheme.of(context).theme.userAvatarNameColors);
final name = getUserName(message.author);
return LinkPreview(
enableAnimation: true,
header: showName ? name : null,
headerStyle: InheritedChatTheme.of(context)
.theme
.userNameTextStyle
.copyWith(color: color),
linkStyle: bodyLinkTextStyle ?? bodyTextStyle,
metadataTextStyle: linkDescriptionTextStyle,
metadataTitleStyle: linkTitleTextStyle,
onPreviewDataFetched: _onPreviewDataFetched,
padding: EdgeInsets.symmetric(
horizontal:
InheritedChatTheme.of(context).theme.messageInsetsHorizontal,
vertical: InheritedChatTheme.of(context).theme.messageInsetsVertical,
),
previewData: message.previewData,
text: message.text,
textStyle: bodyTextStyle,
width: width,
);
}
Widget _textWidgetBuilder(
types.User user,
BuildContext context,
bool enlargeEmojis,
) {
final theme = InheritedChatTheme.of(context).theme;
final color =
getUserAvatarNameColor(message.author, theme.userAvatarNameColors);
final name = getUserName(message.author);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (showName)
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.userNameTextStyle.copyWith(color: color),
),
),
SelectableText(
message.text,
style: user.id == message.author.id
? enlargeEmojis
? theme.sentEmojiMessageTextStyle
: theme.sentMessageBodyTextStyle
: enlargeEmojis
? theme.receivedEmojiMessageTextStyle
: theme.receivedMessageBodyTextStyle,
textWidthBasis: TextWidthBasis.longestLine,
),
],
);
}
@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: _enlargeEmojis && hideBackgroundOnEmojiMessages
? 0.0
: _theme.messageInsetsHorizontal,
vertical: _theme.messageInsetsVertical,
),
child: _textWidgetBuilder(_user, context, _enlargeEmojis),
);
}
}