How can I parse and replace a word as a hashtag link when user types some text using flutter?

1,367

Assuming that you want to do this once the user has finished posting and not inside the typing bar:

The smart_text_view 0.1.0 package contains something called LinkTextSpan, which you can use to easily implement this on the view. Their example folder contains an example that was implemented.

As shown in the Flutter's Official Implementation here for the Flutter Gallery Drawyer, you can do this:

_LinkTextSpan(
                style: linkStyle,
                url: 'https://something.com',
                text: 'flutter github repo',
              ),

and

_LinkTextSpan({ TextStyle style, String url, String text }) : super(
    style: style,
    text: text ?? url,
    recognizer: TapGestureRecognizer()..onTap = () {
      launch(url, forceSafariVC: false);
    }
  );
Share:
1,367
Dead Pool
Author by

Dead Pool

Updated on December 10, 2022

Comments

  • Dead Pool
    Dead Pool over 1 year

    I want when a user types some text to make a post. But the thing is when the user types a hashtag (e.g. #avengers) I can parse and replace that hashtag with a link or ontap widget... Same goes with URL links

    • Phuc Tran
      Phuc Tran about 5 years
      The hashtag is in the TextField also? Or it is in the post?
    • Dead Pool
      Dead Pool about 5 years
      inside the textfield
    • Phuc Tran
      Phuc Tran about 5 years
      Can you share the design of what you want?
    • Dead Pool
      Dead Pool about 5 years
      @PhucTran it works the same way like facebook, youtube and instagram's hashtag system
    • Dead Pool
      Dead Pool about 5 years
      @PhucTran I would show code but it's a lot
    • Shangari C
      Shangari C almost 5 years
      @DeadPool Did you find the solution about mentions,HashTag.can you pls suggest related to that.
    • Shangari C
      Shangari C almost 5 years
      @PhucTran Can you pls suggest an idea to implement mentions,hastags how to identify @,# to show popup list.
  • kilokahn
    kilokahn over 4 years
    Can you elaborate a bit more about how this works - a snippet of code demonstrating how you would implement smart_text_view to parse hashtags will go a long way in helping me understand this better.