[英]How do I search in Flutter DropDown button
我在本地 json 中有一个国家名称列表。 我可以加载我的本地 json 并分配给DropDown button 。 json 文件中有 193 个国家/地区,例如。 如下所示。 如果我想选择美国,用户必须一直向下滚动。 如何输入国家名称,例如; 如果我用户输入 U 或 u,下拉菜单可以进行快速过滤并列出所有以 U 开头的国家/地区,例如美国。 如何在 Flutter DropDownbutton 项目中搜索?
{
"country": [
{
"countryCode": "AD",
"countryName": "Andorra",
"currencyCode": "EUR",
"isoNumeric": "020"
},
{
"countryCode": "AE",
"countryName": "United Arab Emirates",
"currencyCode": "AED",
"isoNumeric": "784"
},
{
"countryCode": "AF",
"countryName": "Afghanistan",
"currencyCode": "AFN",
"isoNumeric": "004"
},
//...
]
}
您可以改用 searchable_dropdown 包: https : //pub.dev/packages/searchable_dropdown
这是我的示例代码searchable_dropdown 不适用于类列表
如果您使用类似我的示例的类列表,请确保输入以下内容
@override
String toString() {
return this.key;
}
一种方法是使用TextEditingController
像这样过滤你的ListView
:
class YourPage extends StatefulWidget {
@override
State createState() => YourPageState();
}
class YourPageState extends State<YourPage> {
List<Country> countries = new List<Country>();
TextEditingController controller = new TextEditingController();
String filter;
@override
void initState() {
super.initState();
//fill countries with objects
controller.addListener(() {
setState(() {
filter = controller.text;
});
});
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return new Material(
color: Colors.transparent,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Padding(
padding: new EdgeInsets.only(top: 8.0, left: 16.0, right: 16.0),
child: new TextField(
style: new TextStyle(fontSize: 18.0, color: Colors.black),
decoration: InputDecoration(
prefixIcon: new Icon(Icons.search),
suffixIcon: new IconButton(
icon: new Icon(Icons.close),
onPressed: () {
controller.clear();
FocusScope.of(context).requestFocus(new FocusNode());
},
),
hintText: "Search...",
),
controller: controller,
)),
new Expanded(
child: new Padding(
padding: new EdgeInsets.only(top: 8.0),
child: _buildListView()),
)
],
));
}
Widget _buildListView() {
return ListView.builder(
itemCount: countries.length,
itemBuilder: (BuildContext context, int index) {
if (filter == null || filter == "") {
return _buildRow(countries[index]);
} else {
if (countries[index].countryName
.toLowerCase()
.contains(filter.toLowerCase())) {
return _buildRow(countries[index]);
} else {
return new Container();
}
}
});
}
Widget _buildRow(Country c) {
return new ListTile(
title: new Text(
c.countryName,
),
subtitle: new Text(
c.countryCode,
));
}
}
我找到了另一个插件。 它有很多功能(例如InputDecoration
),它看起来像TextFormField()
。 所以你的用户界面将是相同的。
我推荐你使用这个神奇的插件: dropdown_search
使用 textfield_search: ^0.8.0插件视图示例
TextFieldSearch(
decoration: InputDecoration(
hintText: "Search",
prefixIcon: Icon(
Icons.search,
color: Colors.black45,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[200],
),
initialList: constants.VEHICLELIST,
label: "label",
controller: selectedVehicle),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.