Home > Net >  How can I search in CheckboxListTile in Flutter?
How can I search in CheckboxListTile in Flutter?

Time:11-19

I am using a CheckboxListTile structure. But since I will have more than 100 Checkboxes in the future, I want to search them, but I could not find any source.

It is easy to search on normal lists on the Internet, but I have never searched an array of type Map<String, bool> List before.

What I want to do is to sort the key elements of the list when I type their names into a small textbox.

What path should I follow. Thanks.

enter image description here

Map<String, bool> List = {
"Kullanıcı 01": false,
"Kullanıcı 02": false,
"Kullanıcı 03": false,
"Kullanıcı 04": false,
"Kullanıcı 05": false,
"Kullanıcı 06": false,
"Kullanıcı 07": false,};

 child: StatefulBuilder(
                                  builder: (context, _setState) => Column(
                                    children: [
                                   
                                      Align(
                                        alignment: Alignment.topLeft,
                                        child: CheckboxListTile(
                                          onChanged: (bool? value2) {
                                            _setState(() {
                                              tumunusecCheckbox = value2!;
                                              if (tumunusecCheckbox == true) {
                                                List.forEach((key, value) {
                                                  _setState(() {
                                                    List.update(
                                                        key, (value) => true);
                                                  });
                                                });
                                              } else {
                                                List.forEach((key, value) {
                                                  _setState(() {
                                                    List.update(key,
                                                        (value) => false);
                                                  });
                                                });
                                              }
                                            });
                                          },
                                          title: Text(
                                            "Tümünü seç",
                                            style: TextStyle(
                                                color: Colors.black87,
                                                fontWeight: FontWeight.bold),
                                          ),
  
                                          value:
                                              tumunusecCheckbox, // foreground
                                        ),
                                      ),
                                      Expanded(
                                        child: ListView(
                                          children: List.keys.map<Widget>((
                                            String key,
                                          ) {
                                            return CheckboxListTile(
                                                value: List[key],
                                                title: Text(
                                                  key,
                                                  style: TextStyle(
                                                      color:
                                                          it_tool_main_blue,
                                                      fontSize: 17.0,
                                                      fontFamily:
                                                          'sans-bold.ttf',
                                                      fontWeight:
                                                          FontWeight.bold),
                                                ),
                                                activeColor:
                                                    it_tool_main_blue,
                                                checkColor: Colors.white,
                                                onChanged: (bool? value) {
                                                  _setState(() {
                                                    List[key] = value!;
                                                    if (value == false) {
                                                   
                                                    } else {
                                                     
                                                    }
                                                  });
                                                });
                                          }).toList(),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),

CodePudding user response:

Even though you said "What I want to do is to sort the key elements of the list when I type their names into a small textbox.", the rest of your question suggests that you want to select matching items from a list.

Presuming this is what you want, pseudo code for a method that returns a map of items that match the key might look like this.

You call this method with the original Map and a string that you want to match to, and the method returns a list of matching items.

Match is whatever you define as a match: partial string match, first letters, soundex, regex. That part is up to you.

Map<String, String> findMatches(Map<String, String> myMap, String matchToThis) {
  Map<String, String> foundItems = {};
  myMap.forEach((key, value) {
     if (KeyMatches) {
       foundItems[key] = value;
     }
  }
  return foundItems
}

PS:

You've created a variable named 'List' that is a map. You might want to pick a better name for this variable. That threw me off for a second.

Map<String, bool> List = {

CodePudding user response:

Thanks for help but, i dont know how can i use this function. This is my try.

 TextFormField(
                          controller: _controller,
                          onChanged: findMatches(myMap, _controller.text),
                        ),




Map<String, String> findMatches(Map<String, bool> myMap, String matchToThis) {
Map<String, bool> foundItems = {};
myMap.forEach(
  (key, value) {
    if (KeyMatches) {
      print("lel");
      foundItems[key] = value;
    }
  },
);
 return foundItems;}}
  • Related