1.使用控件搜索加下拉框dropdown_search: ^0.4.9和获取中文拼音lpinyin: ^1.1.1
2.加入中文查询和首字查询
在当中找到相应的packages,再在SelectDialog.dart当中加入引入拼音搜索
import 'package:lpinyin/lpinyin.dart';
更改匹配方法manageItemsByFilter使其可以支持拼音首字搜索
void manageItemsByFilter(String filter, {bool isFistLoad = false}) async {
_loadingNotifier.value = true;
List<T> applyFilter(String filter) {
return _items.where((i) {
if (widget.filterFn != null)
return (widget.filterFn(i, filter));
else if (widget.itemAsString != null) {
if(filter.contains(new RegExp('^[a-zA-Z]+'))){
return (PinyinHelper.getFirstWordPinyin(widget.itemAsString(i)))
?.toLowerCase()
?.contains(filter.toLowerCase()) ??
false;
}else{
return (widget.itemAsString(i))
?.toLowerCase()
?.contains(filter.toLowerCase()) ??
false;
}
}
else if (i.toString().toLowerCase().contains(filter.toLowerCase()))
return true;
return false;
}).toList();
}
//load offline data for the first time
if (isFistLoad && widget.items != null) _items.addAll(widget.items);
//manage offline items
if (widget.onFind != null && (widget.isFilteredOnline || isFistLoad)) {
try {
final List<T> onlineItems = List();
onlineItems.addAll(await widget.onFind(filter) ?? List());
//Remove all old data
_items.clear();
//add offline items
if (widget.items != null) {
_items.addAll(widget.items);
//if filter online we filter only local list based on entred keyword (filter)
if (widget.isFilteredOnline == true) {
var filteredLocalList = applyFilter(filter);
_items.clear();
_items.addAll(filteredLocalList);
}
}
//add new online items to list
_items.addAll(onlineItems);
_addDataToStream(applyFilter(filter));
} catch (e) {
_addErrorToStream(e);
//if offline items count > 0 , the error will be not visible for the user
//As solution we show it in dialog
if (widget.items != null && widget.items.isNotEmpty) {
_showErrorDialog(e);
_addDataToStream(applyFilter(filter));
}
}
} else {
_addDataToStream(applyFilter(filter));
}
_loadingNotifier.value = false;
}
3.使用
DropdownSearch<User>(
items: nurses, //user集合
maxHeight: 300,
selectedItem: selectUser(val), //通过id查到对应的user类
itemAsString: (User u) => u.name, //显示user的名字
onChanged: (value){
onDropChange(value.id);
},
showSearchBox: true,
showAsSuffixIcons: true,
)