flutter_chat_ui icon indicating copy to clipboard operation
flutter_chat_ui copied to clipboard

Add an onLinkClicked feature

Open microcoder-py opened this issue 1 year ago • 2 comments

Add an onLinkClicked feature

Is your feature request related to a problem?

A clear and concise description of what the problem is or why we should process it. Whenever any link is clicked in the messages being sent, I would like to display them in app instead of redirecting to external browser. I think it will be useful for clicktracking as well

What solution would you suggest?

A clear and concise description of what you want to happen. How would it solve the problem of yours?

Is there any additional solution to that?

A clear and concise description of any alternative solutions or features you've considered. Could not find any

Extras

Screenshots or videos 📸 If applicable, add screenshots or videos to help explain your feature.

Code snippets 📝 If applicable, add code samples to help explain your feature.

// Your snippet here...

Related issues/PRs

Let us know if this is related to any issue/pull request.

microcoder-py avatar Sep 07 '23 12:09 microcoder-py

Hi there, Have you find a way to intercept the link before it gets opened in the device browser? I gotta open it in-app... I have it to work if the message (bubble) is tapped but not if it's the link being hit directly... Any luck?

SwiftyFlow avatar Feb 03 '24 08:02 SwiftyFlow

Found a way... In your Chat() where you have the

messages: _messages,
onAttachmentPressed: _handleAttachmentPressed,
onMessageTap: _handleMessageTap,
onPreviewDataFetched: _handlePreviewDataFetched,
onSendPressed: _handleSendPressed,

I have added:

usePreviewData: false, //(not sure it's required)
textMessageOptions: const TextMessageOptions(isTextSelectable: false),

Then it's _handleMessageTap working for me

if (message is types.TextMessage) {
      var link = '';

      final tempLink = message.previewData?.link;
      if (tempLink != null) {
        link = tempLink;
      } else {
        RegExp exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
        Iterable<RegExpMatch> matches = exp.allMatches(message.text);

        if (matches.isNotEmpty) {
          link = message.text.substring(matches.first.start, matches.first.end);
        }
      }

      debugPrint('link in message: ${link != '' ? link : 'NO LINK FOUND'}');

      Navigator.of(context).pushNamed(WebViewScreen.routeName);
}else ...

SwiftyFlow avatar Feb 03 '24 08:02 SwiftyFlow