首页 文章

如何在Flutter DropDown按钮中搜索

提问于
浏览
0

我在本地json中有一个国家名称列表 . 我可以加载我的本地json并分配给 DropDown button . 在json文件中有193个国家作为前 . 如下所示 . 如果我想选择United State,用户必须一直向下滚动 . 如何输入国家名称等;如果我用户输入U或u,则下拉列表可以快速过滤并列出以U开头的所有国家/地区,例如United State . How do I search in Flutter DropDownbutton items

{
    "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"
            },

1 回答

  • 0

    一种方法是使用 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,
            ));
      }
    }
    

相关问题