How do I search in Flutter DropDown button











up vote
0
down vote

favorite












I have a list of the countries name in local json. I can load my local json and assign to DropDown button. there is a 193 countries in json file as ex. shown below. If I want to select United State, user have to scroll all the way down. How can enter a countries name such as; if I user enter U or u the dropdown can makes quick filtering and list all the countries that starts with U such as 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"
},









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a list of the countries name in local json. I can load my local json and assign to DropDown button. there is a 193 countries in json file as ex. shown below. If I want to select United State, user have to scroll all the way down. How can enter a countries name such as; if I user enter U or u the dropdown can makes quick filtering and list all the countries that starts with U such as 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"
    },









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a list of the countries name in local json. I can load my local json and assign to DropDown button. there is a 193 countries in json file as ex. shown below. If I want to select United State, user have to scroll all the way down. How can enter a countries name such as; if I user enter U or u the dropdown can makes quick filtering and list all the countries that starts with U such as 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"
      },









      share|improve this question















      I have a list of the countries name in local json. I can load my local json and assign to DropDown button. there is a 193 countries in json file as ex. shown below. If I want to select United State, user have to scroll all the way down. How can enter a countries name such as; if I user enter U or u the dropdown can makes quick filtering and list all the countries that starts with U such as 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"
      },






      dart flutter filtering dropdown






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 23 hours ago

























      asked yesterday









      Nick

      1418




      1418
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          One way is to use a TextEditingController to filter your ListView like this:



          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,
          ));
          }
          }





          share|improve this answer





















          • SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
            – Nick
            23 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372156%2fhow-do-i-search-in-flutter-dropdown-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          One way is to use a TextEditingController to filter your ListView like this:



          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,
          ));
          }
          }





          share|improve this answer





















          • SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
            – Nick
            23 hours ago















          up vote
          0
          down vote













          One way is to use a TextEditingController to filter your ListView like this:



          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,
          ));
          }
          }





          share|improve this answer





















          • SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
            – Nick
            23 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          One way is to use a TextEditingController to filter your ListView like this:



          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,
          ));
          }
          }





          share|improve this answer












          One way is to use a TextEditingController to filter your ListView like this:



          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,
          ));
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 23 hours ago









          SnakeyHips

          1959




          1959












          • SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
            – Nick
            23 hours ago


















          • SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
            – Nick
            23 hours ago
















          SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
          – Nick
          23 hours ago




          SnakeyHips you misunderstood my question. My dropdown button inside a ListView, and I assign my data to my dropdown button. I am looking way so I can search or filter the dropdown button with user interaction.
          – Nick
          23 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372156%2fhow-do-i-search-in-flutter-dropdown-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]