Dn-a / flutter_tags

With flutter tags you can create selectable or input tags that automatically adapt to the screen width
https://pub.dartlang.org/packages/flutter_tags
MIT License
507 stars 127 forks source link

Can the suggestions prop not be case-sensitive? #52

Open davidmednikov opened 4 years ago

davidmednikov commented 4 years ago

Is it possible to make the suggestions matching non-case-sensitive?

lowerCase prop and applying toLowerCase() to the suggestions has not worked.

Dn-a commented 4 years ago

@davidmednikov it is not easy to implement

davidmednikov commented 4 years ago

I was able to implement it the following way:

In suggestions_textfield.dart, line 154 and 155 currently do:

_matches =  _suggestions.where((String sgt) => sgt.startsWith(str)).toList();

I replaced this with:

_matches = _suggestions.where((String sgt) => sgt.toLowerCase().startsWith(str.toLowerCase())).toList();

_matches = _matches.map((match) => match.replaceFirst(match.substring(0, str.length), str)).toList();

Then, for the TagsTextField widget:

textField: TagsTextField(
    duplicates: true,
    ...
    onSubmitted: (newTagStr) {
        String tagName = suggestions.firstWhere((suggestion) => suggestion.toLowerCase() == newTagStr.toLowerCase(), orElse: () => null);
        if (tagName != null) {
            newTagStr = tagName;
        }
        if (!tags.map((tag) => tag.title.toLowerCase()).toList().contains(newTagStr.toLowerCase())) {
            setState(() {
                tags.add(Item(
                    index: tags.length,
                    title: newTagStr,
                    active: true,
                    customData: true
                ));
            });
        }
    }
}
Dn-a commented 4 years ago

@davidmednikov yours is a great job! I just wanted to avoid adding all this logic when instead a good practice would be to avoid upstream the use of contemporary uppercase and lowercase text in the suggestions. However, I will take your work into consideration, and carry out checks.