From 153455ae563756830c10d02adbc23919ec346406 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 5 Mar 2020 18:03:14 +0100 Subject: [PATCH] add on mention tap --- lib/src/message_widget.dart | 26 ++++++++++++++++++++++---- pubspec.yaml | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/src/message_widget.dart b/lib/src/message_widget.dart index c90e8376..0fff2959 100644 --- a/lib/src/message_widget.dart +++ b/lib/src/message_widget.dart @@ -37,8 +37,12 @@ class MessageWidget extends StatefulWidget { this.onThreadTap, this.onMessageActions, this.isParent = false, + this.onMentionTap, }) : super(key: key); + /// Function called on mention tap + final void Function(User) onMentionTap; + /// Function called on long press final Function(BuildContext, Message) onMessageActions; @@ -360,7 +364,8 @@ class _MessageWidgetState extends State if (widget.message.text.trim().isNotEmpty) { String text = widget.message.text; widget.message.mentionedUsers?.forEach((u) { - text = text.replaceAll(u.name, '**${u.name}**'); + text = text.replaceAll( + '@${u.name}', '[@${u.name}](@${u.name.replaceAll(' ', '')})'); }); column.children.addAll( @@ -395,9 +400,23 @@ class _MessageWidgetState extends State padding: EdgeInsets.all(10), constraints: BoxConstraints.loose(Size.fromWidth(300)), child: MarkdownBody( - data: '${text}', + data: text, onTapLink: (link) { - _launchURL(link); + if (link.startsWith('@')) { + final mentionedUser = + widget.message.mentionedUsers.firstWhere( + (u) => '@${u.name.replaceAll(' ', '')}' == link, + orElse: () => null, + ); + + if (widget.onMentionTap != null) { + widget.onMentionTap(mentionedUser); + } else { + print('tap on ${mentionedUser.name}'); + } + } else { + _launchURL(link); + } }, styleSheet: MarkdownStyleSheet.fromTheme( Theme.of(context).copyWith( @@ -456,7 +475,6 @@ class _MessageWidgetState extends State return GestureDetector( child: IntrinsicWidth(child: column), onLongPress: () { - print('widget.message.type: ${widget.message.type}'); if (widget.message.type == 'ephemeral') { return; } diff --git a/pubspec.yaml b/pubspec.yaml index cf2c84e6..30f50e06 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,7 +20,7 @@ dependencies: file_picker: ^1.4.3+2 image_picker: ^0.6.3+4 keyboard_visibility: ^0.5.6 - stream_chat: ^0.1.16 + stream_chat: ^0.1.17 dev_dependencies: pedantic: ^1.8.0+1