2000calories / flutter_easy_rich_text

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

Print onTap string #24

Closed SNNafi closed 2 years ago

SNNafi commented 2 years ago

Suppose I colored some text using regex. Example:

Text  يَنْصُرُ More text

Text وَاحِدٌ مُذَكَّرٌ غَائِبٌ text يَنْصُرُ text

Here I have colored arabic text. When I tap on these arabic text, it gives me a message by

recognizer: TapGestureRecognizer()
                        ..onTap = () {

                          print("Tap recognizer to print this sentence.");
    },

BUT, is there any way to get the tapped arabic text? Like when tapped on يَنْصُرُ it prints يَنْصُرُ . When وَاحِدٌ مُذَكَّرٌ غَائِبٌ , prints وَاحِدٌ مُذَكَّرٌ غَائِبٌ

2000calories commented 2 years ago

Is this what you want?

  var targetString1 = "tartget 1";
  var targetString2 = "tartget 2";

  EasyRichText(
    "Tap recognizer to print tartget 1.Tap recognizer to print tartget 2.",
    patternList: [
      EasyRichTextPattern(
        targetString: targetString1,
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            print(targetString1);
          },
        style: TextStyle(
          decoration: TextDecoration.underline,
        ),
      ),
      EasyRichTextPattern(
        targetString: targetString2,
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            print(targetString2);
          },
        style: TextStyle(
          decoration: TextDecoration.underline,
        ),
      ),
    ],
  ),
SNNafi commented 2 years ago

Thanks for the quick response. BUT, you have hardcoded the texts. In my case these are dynamic. So there is no way to know them before. I just to get the tapped the word when it is tapped dynamically. Is it possible ?

2000calories commented 2 years ago

How you colored these words?

SNNafi commented 2 years ago

Like this

EasyRichTextPattern(
                            targetString:
                                "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufbc1]|[\ufbd3-\ufd3f]|[\ufd50-\ufd8f]|[\ufd92-\ufdc7]|[\ufe70-\ufefc]|[\uFDF0-\uFDFD]",
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {

                              },
                            style: GoogleFonts.amiri(
                                textStyle: TextStyle(
                                    color: Colors.white,
                                    fontSize: 34,
                                    fontStyle: FontStyle.normal)),
                          ),
2000calories commented 2 years ago

You can use matchBuilder to get every word that match the regex expression and then implement TapGestureRecognizer. However, the behaviour of Arabic words in regex is strange to me. I cannot find the word boundary for arabic character.

  EasyRichText(
    "Text وَاحِدٌ مُذَكَّرٌ غَائِبٌ text يَنْصُرُ text",
    patternList: [
      EasyRichTextPattern(
        targetString: "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufbc1]|[\ufbd3-\ufd3f]|[\ufd50-\ufd8f]|[\ufd92-\ufdc7]|[\ufe70-\ufefc]|[\uFDF0-\uFDFD]",
        matchBuilder:
            (BuildContext context, RegExpMatch match) {
          var targetString = match[0];
          return TextSpan(
            text: targetString,
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                print(targetString);
              },
            style: TextStyle(color: Colors.red),
          );
        },
      ),
    ],
  ),
SNNafi commented 2 years ago

Thank you. I will try. In android using \p{InArabic}+ gives you every arabic word. That was quite easy. But in dart, it is quite hard. Every pattern I have found has a different result !. And the above i.e \p{InArabic}+ doesn't work in dart .