Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to add the user input value even if the value is not found. #121

Open
Shiba-Kar opened this issue Jul 15, 2022 · 2 comments

Comments

@Shiba-Kar
Copy link

Shiba-Kar commented Jul 15, 2022

Like the implementation done in flutter_tagging_plus: ^4.0.1

DEMO

@Shiba-Kar
Copy link
Author

Come Up with this solution

FormBuilderChipsInput<String>(
                  decoration: InputDecoration(
                    labelText: 'Skills',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10.w),
                      borderSide: BorderSide.none,
                    ),
                  ),
                  name: 'skills',
                  maxChips: 5,
                  findSuggestions: (String query) {
                    if (query.isNotEmpty) {
                      var lowercaseQuery = query.toLowerCase();
                      List<String> res = skills.where((skill) {
                        return skill
                            .toLowerCase()
                            .contains(query.toLowerCase());
                      }).toList(growable: false)
                        ..sort(
                          (a, b) =>
                              a.toLowerCase().indexOf(lowercaseQuery).compareTo(
                                    b.toLowerCase().indexOf(lowercaseQuery),
                                  ),
                        );
                      if (res.isNotEmpty) return res;

                      return ['Add $query'];
                    } else {
                      return const [];
                    }
                  },
                  chipBuilder: (context, state, skill) {
                    return InputChip(
                      key: ObjectKey(skill),
                      label: Text(skill),
                      /*  avatar: CircleAvatar(
                    backgroundImage: NetworkImage(skill.imageUrl),
                                    ), */
                      onDeleted: () => state.deleteChip(skill),
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    );
                  },
                  suggestionBuilder: (context, state, skill) {
                    final bool isAddSkillPresent = skill.startsWith('Add');
                    final formattedSkill = skill.replaceAll('Add', '');
                    return Container(
                      alignment: Alignment.centerLeft,
                      child: InkWell(
                        onTap: () => state.selectSuggestion(formattedSkill),
                        child: Chip(
                          key: ObjectKey(skill),
                          // onDeleted: () => state.selectSuggestion(skill),
                          label: Text(
                            skill,
                            style: TextStyle(
                              fontWeight: isAddSkillPresent
                                  ? FontWeight.bold
                                  : FontWeight.normal,
                            ),
                          ),
                          // avatar: const CircleAvatar(),
                        ),
                      ),
                    );
                   
                  },
                  allowChipEditing: true,
                  suggestionsBoxMaxHeight: 300.h,
                ),

@AAverin
Copy link

AAverin commented Jun 10, 2023

A bit simpler solution:

return ChipsInput<String>(
                          decoration: const InputDecoration(
                            labelText: "Tags (optional)",
                            hintText:
                                "Type to get suggestions or add a new tag",
                          ),
                          chipBuilder: (context, state, item) {
                            return InputChip(
                              key: ObjectKey(item),
                              label: Text(item),
                              onDeleted: () => state.deleteChip(item),
                              materialTapTargetSize:
                                  MaterialTapTargetSize.shrinkWrap,
                            );
                          },
                          suggestionBuilder: (context, state, item) {
                            return ListTile(
                              key: ObjectKey(item),
                              title: Text(item),
                              onTap: () => state.selectSuggestion(item),
                            );
                          },
                          findSuggestions: (String query) {
                            if (query.isNotEmpty) {
                              final suggestions = {
                                tags.firstWhere(
                                    (element) => element.name
                                        .toLowerCase()
                                        .contains(query.toLowerCase()),
                                    orElse: () => query) // <= here we are "adding" the new entry to suggestions list
                              };
                              if (suggestions.isNotEmpty) {
                                return {...suggestions, HabitTag(name: query)} // <= here we make sure there is no duplicate 
                                    .toList();
                              }
                            }
                            return [];
                          },
                          onChanged: (t) {
                            print(t);
                          },
                        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants