Crazelu / fluttertagger

A Flutter package that allows for the extension of TextFields to provide tagging capabilities (e.g @ user mentions and # hashtags).
MIT License
35 stars 16 forks source link

If add "#" when click button, cannot trigger search #8

Closed Amber916Young closed 1 month ago

Amber916Young commented 1 month ago

Screenshot 2024-08-01 at 16 15 07

If I add "#" when clicking a button, it cannot trigger the search function and will also change the text style. like, add an emoji also will change the format of the text

Screenshot 2024-08-01 at 16 17 14 Screenshot 2024-08-01 at 16 17 22
Crazelu commented 1 month ago

There are two things here:

Firstly, FlutterTaggerController works with text and formattedText. The text is what's displayed in the TextField. The formattedText contains more information that the widget uses to identifies tags. So if you have text with tags already formatted and you want to append to it through the controller, you need to work with the formattedText.

It'll look like this:

String formattedText = controller.formattedText;
controller.text = formattedText += '👍'; // adds 👍

// update text selection
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

// then call formatTags on the controller to preserve formatting
controller.formatTags();

Secondly, you have just identified a bug in the library. Because if you follow the steps above, you still wouldn't be able to trigger a search (but you'll get formatting to work). I'll fix that shortly. However, it's important to note that entering a trigger character only activates the search. You have to enter another character after the trigger to actually begin the search. By that I mean:

String formattedText = controller.formattedText;

controller.text = formattedText += '#'; 
// adds #. this WILL NOT trigger search

// update text selection
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

controller.text = formattedText += 'a'; 
// adds a. this WILL trigger search with query string 'a'

// update text selection
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

// then call formatTags on the controller to preserve formatting
controller.formatTags();
Crazelu commented 1 month ago

@Amber916Young I've published v2.1.1 on pub.dev with the fix for the issue with triggering search. Also updated the README to guide on how to do what you've pointed out.

Let me know if this fixes your issue.

Amber916Young commented 1 month ago

@Amber916Young I've published v2.1.1 on pub.dev with the fix for the issue with triggering search. Also updated the README to guide on how to do what you've pointed out.

Let me know if this fixes your issue.

Thank you very much for your detailed response. I will ensure the search issue is updated in the new version😀

Crazelu commented 1 month ago

You're welcome!

And thanks for pointing that out. I'll be closing this now. Feel free to reopen if necessary.