[英]from dropdownmenu to dropdownsearch in flutter
I was using normal dropdown menu in my project with small amount of data fetched from my api, but now i have menu which could reach hundred of values and make it hard to select an item.我在我的项目中使用普通下拉菜单,从我的 api 获取少量数据,但现在我的菜单可以达到数百个值,并且很难 select 一个项目。 That's why i wanted to use DropDownSearch but instead i get an error
这就是为什么我想使用 DropDownSearch 但我得到一个错误
Normal dropdown code which works very fine正常的下拉代码,效果很好
DropdownButton(
showSearchBox: true,
showSelectedItem: true,
items: data3.map((item) {
return new DropdownMenuItem(
child: Text(item['first_name']+" "+ item['last_name']),
value: item['emp_code'].toString(),
);
}).toList(),
onChanged: (newVal) {
setState(() {
_mySelection3 = newVal.toString();
});
},
value: _mySelection3,
),
data3 = [{emp_code: 111, first_name: adnen, last_name: hamouda}, {emp_code: 666, first_name: ahmed, last_name: ahmed 99}.... data3 = [{emp_code: 111, first_name: adnen, last_name: hamouda}, {emp_code: 666, first_name: ahmed, last_name: ahmed 99}....
this is the result: normal dropdown这是结果:正常下拉
But when i tried to convert it to dropDownSearch i got this result: search dropdown I want to show the first_name and last_name like the normal dropdown but save the value of their 'emp_code' which i will be using in another api later.但是当我尝试将其转换为 dropDownSearch 时,我得到了这个结果:搜索下拉列表我想像普通下拉列表一样显示名字和姓氏,但保存他们的“emp_code”的值,我稍后将在另一个 api 中使用。 How can i fix it?
我该如何解决?
DropdownSearch(
mode: Mode.DIALOG,
showSearchBox: true,
items: data3.map((item) {
return new DropdownMenuItem(
child: Text(item['first_name']+" "+ item['last_name']),
value: item['emp_code'].toString(),
);
}).toList(),
onChanged: (newVal) {
setState(() {
print(data3);
_mySelection3 = newVal.toString();
});
},
selectedItem: _mySelection3,
),
Here is the way I found to use searchable dropdown lists.这是我发现使用可搜索下拉列表的方式。
I tried dropdown_search package at first, but it seems the documentation and examples related with the latest version (5.0.2) are depreciated.起初我尝试了dropdown_search package,但似乎与最新版本(5.0.2)相关的文档和示例已被贬值。 Later on, I moved to dropdown_button2 and I am happy the way the
DropdownButtonFormField2
was implemented, because it is very similar which I have done to the flutter DropdownButtonFormField
implementation, so far.后来,我搬到了dropdown_button2 ,我很高兴
DropdownButtonFormField2
的实现方式,因为它与我迄今为止所做的 flutter DropdownButtonFormField
实现非常相似。
Have a look at the Flutter bundled DropdownButtonFormField example:看看 Flutter 捆绑的 DropdownButtonFormField 示例:
return DropdownButtonFormField<SetorTemp>(
decoration: const InputDecoration(
labelText: 'Setor institucional'),
isExpanded: true,
icon: const Icon(Icons.arrow_downward),
items: snapshot.data!
.map((SetorTemp rtItem) =>
DropdownMenuItem<SetorTemp>(
value: rtItem,
child: Text(
'${rtItem.nome} (${rtItem.sigla})',
softWrap: true,
),
))
.toList(),
hint: Text(
'${selectedSetorTemp.nome} (${selectedSetorTemp.sigla})'),
onChanged: (SetorTemp? newValue) {
setState(() {
// do your logic here!
selectedSetorTemp = newValue;
});
},
);
And the DropdownButtonFormField2 using dropdown_button2 package DropdownButtonFormField2 使用 dropdown_button2 package
return DropdownButtonFormField2<SetorTemp>(
decoration: const InputDecoration(
labelText: 'Setor institucional'),
isExpanded: true,
icon: const Icon(Icons.arrow_downward),
items: snapshot.data!
.map((SetorTemp rtItem) =>
DropdownMenuItem<SetorTemp>(
value: rtItem,
child: Text(
'${rtItem.nome} (${rtItem.sigla})',
softWrap: true,
),
))
.toList(),
hint: Text(
'${selectedSetorTemp.nome} (${selectedSetorTemp.sigla})'),
onChanged: (SetorTemp? newValue) {
setState(() {
// Do your logic here!
selectedSetorTemp = newValue;
});
},
// Search implementation using dropdown_button2 package
searchController: searchContentSetor,
searchInnerWidget: Padding(
padding: const EdgeInsets.only(
top: 8,
bottom: 4,
right: 8,
left: 8,
),
child: TextFormField(
controller: searchContentSetor,
decoration: InputDecoration(
isDense: false,
contentPadding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 8,
),
labelText: 'Setor institucional',
hintText: 'Parte do nome do setor...',
counterText: '',
hintStyle: const TextStyle(fontSize: 16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
searchMatchFn: (rtItem, searchValue) {
return (rtItem.value.nome
.toLowerCase()
.contains(searchValue.toLowerCase()));
},
//This to clear the search value when you close the menu
onMenuStateChange: (isOpen) {
if (!isOpen) {
searchContentSetor.clear();
}
},
);
They are similar at first:起初它们是相似的:
However, after clicking they will show their differences from the initial implementation:但是,单击后,它们将显示与初始实现的差异:
To the dropdown_button2 package:到 dropdown_button2 package:
The selectedSetorTemp
variable is of the type SetorTemp
and the model I have used is: selectedSetorTemp
变量是SetorTemp
类型,我使用的 model 是:
class SetorTemp {
final int id;
final String? nome;
final String? sigla;
SetorTemp({required this.id, this.nome, this.sigla});
factory SetorTemp.fromJson(Map<String, dynamic> json) {
return SetorTemp(
id: json['id'] as int,
nome: json['nome'] as String,
sigla: json['sigla'] as String,
);
}
Map<String, dynamic> toJson() => {
'nome': nome,
'sigla': sigla,
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.