[英]How can I search in CheckboxListTile in Flutter?
I am using a CheckboxListTile structure.我正在使用 CheckboxListTile 结构。 But since I will have more than 100 Checkboxes in the future, I want to search them, but I could not find any source.
但是由于我以后会有100多个Checkboxes,我想搜索它们,但是我找不到任何来源。
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.在网上搜索普通列表很容易,但我之前从未搜索过 Map<String, bool> List 类型的数组。
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.
谢谢。
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(),
),
),
],
),
),
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.即使您说“我想要做的是在我将名称输入到一个小文本框中时对列表的关键元素进行排序。”,您问题的 rest 表明您想要select匹配列表中的项目。
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.假设这是您想要的,返回与键匹配的项目的 map 的方法的伪代码可能如下所示。
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.您使用原始 Map 和要匹配的字符串调用此方法,该方法返回匹配项的列表。
Match is whatever you define as a match: partial string match, first letters, soundex, regex.匹配是您定义为匹配的任何内容:部分字符串匹配、首字母、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: PS:
You've created a variable named 'List' that is a map.您已经创建了一个名为“List”的变量,它是一个 map。 You might want to pick a better name for this variable.
您可能想为这个变量选择一个更好的名称。 That threw me off for a second.
这让我愣了一秒。
Map<String, bool> List = {
Thanks for help but, i dont know how can i use this function.感谢您的帮助,但我不知道如何使用这个 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;}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.