desconexo / highlight_text

A flutter plugin to highlight words from a text
MIT License
30 stars 22 forks source link

Highlighting URLs using highlight_text package based on some logic #27

Closed cdotdkgupta closed 2 years ago

cdotdkgupta commented 2 years ago

I'm developing a mobile application in flutter. It needs to highlight some urls present in a string based on some logic. I find highlight_text package to do the task. This takes text and words that is to be highlighted. Everything is working fine except when i'm passing text that contains url followed by some number. It is throwing error as following:

Another exception was thrown: RangeError (index): Invalid value: Only valid value is 0: 6777 text : In a meeting, will call you later. On my wa'y Please call me later www.something.in 6777 word : www.something.in

But when url is followed by some number and then by some text, it does not throw exception and works well.

text : In a meeting, will call you later. On my way Please call me later www.something.in 6777 test text

word : www.something.in

here is my code snippet

Map<String, bool> textContent = {'www.something1.in': true,'www.something2.in': false};
                        Map<String, HighlightedWord> words = {};
                         textContent.forEach((key, value) {
                              if (value) {
                                words[key] = HighlightedWord(
                                  onTap: () {
                                    debugPrint(key);
                                  },
                                  textStyle: const TextStyle(
                                      decoration: TextDecoration.underline,
                                      color: Colors.red,
                                      fontSize: 13,
                                      fontWeight: FontWeight.bold),
                                );
                                imageURL = 'images/mal.png';
                              } else {
                                words[key] = HighlightedWord(
                                  onTap: () {
                                    debugPrint(key);
                                  },
                                  textStyle: const TextStyle(
                                      decoration: TextDecoration.underline,
                                      color: Colors.blue,
                                      fontSize: 13),
                                );
                              }
                            });  

Above code snippets preparing words that is to be highlighted.

TextHighlight(
                      text: text,
                      words: words,
                      maxLines: 3,
                      textStyle: const TextStyle(
                        fontSize: 13.5,
                        overflow: TextOverflow.clip,
                        color: Colors.black,
                      ),
                    ),

This is where i am passing text and words. Exception is throwing at line with keyword TextHighlight. There are text of exception

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following RangeError was thrown building TextHighlight(dirty): RangeError (index): Invalid value: Only valid value is 0: 6777

Can someone please tell me what's the issue with my code? I have exhausted debugging it. It’ll be great help.

desconexo commented 2 years ago

Hi, just fixed it. But I cannot upload it to pub.dev at the moment, so you'll need to import the project from github