2000calories / flutter_easy_rich_text

The EasyRichText widget provides an easy way to use RichText.
https://pub.dev/packages/easy_rich_text
MIT License
80 stars 33 forks source link

want to use recoginizer with captured string #11

Closed ogata-k closed 3 years ago

ogata-k commented 3 years ago

i want to use recoginizer with captured string.

now: EasyRichTextPattern( recognizer: TapGestureRecognizer() ..onTap = (){ ... } );

change to: EasyRichTextPattern( recognizer: (String captured) => TapGestureRecognizer() ..onTap = (){ ... } );

can do it ?

2000calories commented 3 years ago

What do you mean "captured string"?

ogata-k commented 3 years ago

url, email, tel number and so on

2000calories commented 3 years ago

the captured string is not the targetString?

ogata-k commented 3 years ago

No. "captured string" is the str variable in this default url-luncher function in flutter_easy_rich_text library.

TapGestureRecognizer tapGestureRecognizerForUrls(String str, String urlType) {
    TapGestureRecognizer tapGestureRecognizer;
    switch (urlType) {
      case 'web':
        if (str.substring(0, 4) != "http") {
          str = "https://$str";
        }
        tapGestureRecognizer = TapGestureRecognizer()
          ..onTap = () {
            _launchURL(str);
          };
        break;
      case 'email':
        tapGestureRecognizer = TapGestureRecognizer()
          ..onTap = () {
            _launchURL("mailto:$str");
          };
        break;
      case 'tel':
        tapGestureRecognizer = TapGestureRecognizer()
          ..onTap = () {
            _launchURL("tel:$str");
          };
        break;
      default:
    }
    return tapGestureRecognizer;
  }

if targetString is regex pattern's string, then captured string by the regex.

2000calories commented 3 years ago

Sorry for the late response. Basically the str variable is the targerString. If not, the recognizer will not be triggered.

ogata-k commented 3 years ago

Then, if urlType isurl, can I use a self-defined function of type Future <void> Function (String)? I want to use targetString as arg of the self-defined function.

2000calories commented 3 years ago

If you do not want the default url behaviour then do not use the urlType option, do what you want in the recognizer.

printTargetString(targetString) {
  print(targetString);
}

var targetString = "https://pub.dev/packages/easy_rich_text";

EasyRichText(
  "Here is a website https://pub.dev/packages/easy_rich_text. Here is a email address test@example.com. Here is a telephone number +852 12345678.",
  patternList: [
    EasyRichTextPattern(
      targetString: targetString,
      // urlType: 'web',if you do not want the default behaviour then do not use this option, do what you want in the recognizer.
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
      recognizer: TapGestureRecognizer()
        ..onTap = () => printTargetString(targetString),
    ),
  ],
),
ogata-k commented 3 years ago

Is it same the way when use regex?