forked from GetStream/stream-chat-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_text.dart
More file actions
74 lines (65 loc) · 2.16 KB
/
message_text.dart
File metadata and controls
74 lines (65 loc) · 2.16 KB
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
import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
/// {@template streamMessageText}
/// The text content of a message.
/// {@endtemplate}
class StreamMessageText extends StatelessWidget {
/// {@macro streamMessageText}
const StreamMessageText({
super.key,
required this.message,
required this.messageTheme,
this.onMentionTap,
this.onLinkTap,
});
/// Message whose text is to be displayed
final Message message;
/// The action to perform when a mention is tapped
final void Function(User)? onMentionTap;
/// The action to perform when a link is tapped
final void Function(String)? onLinkTap;
/// [StreamMessageThemeData] whose text theme is to be applied
final StreamMessageThemeData messageTheme;
@override
Widget build(BuildContext context) {
final streamChat = StreamChat.of(context);
assert(streamChat.currentUser != null, '');
return BetterStreamBuilder<String>(
stream: streamChat.currentUserStream.map((it) => it!.language ?? 'en'),
initialData: streamChat.currentUser!.language ?? 'en',
builder: (context, language) {
final messageText = message
.translate(language)
.replaceMentions()
.text
?.replaceAll('\n', '\n\n')
.trim();
return StreamMarkdownMessage(
data: messageText ?? '',
messageTheme: messageTheme,
selectable: isDesktopDeviceOrWeb,
onTapLink: (
String text,
String? href,
String title,
) {
if (text.startsWith('@')) {
final mentionedUser = message.mentionedUsers.firstWhereOrNull(
(u) => '@${u.name}' == text,
);
if (mentionedUser == null) return;
onMentionTap?.call(mentionedUser);
} else if (href != null) {
if (onLinkTap != null) {
onLinkTap!(href);
} else {
launchURL(context, href);
}
}
},
);
},
);
}
}