How to Solve DropDown Selection Is Not Displaying In Flutter ?

· 3 min read
How to Solve DropDown Selection Is Not Displaying In Flutter
How to Solve DropDown Selection Is Not Displaying In Flutter

When users needed to select a single item from a list of the available items at that time DropDown List is used. So In this article, we will go through How to Solve DropDown Selection is Not Displaying In Flutter?

How to Solve DropDown Selection Is Not Displaying In Flutter?

Users can give try to below code.

var currentSelectedValue;
const deviceTypes = ["Mac", "Windows", "Mobile"];

 Widget typeFieldWidget() {
return Container(
  padding: EdgeInsets.symmetric(horizontal: 20),
  child: FormField<String>(
    builder: (FormFieldState<String> state) {
      return InputDecorator(
        decoration: InputDecoration(
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(5.0))),
        child: DropdownButtonHideUnderline(
          child: DropdownButton<String>(
            hint: Text("Select Device"),
            value: currentSelectedValue,
            isDense: true,
            onChanged: (newValue) {
              setState(() {
                currentSelectedValue = newValue;
              });
              print(currentSelectedValue);
            },
            items: deviceTypes.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
          ),
        ),
      );
    },
  ),
);
}

User will get output like the below:

DropDownList Selection In Flutter
DropDownList Selection In Flutter

Declare a new class at the top of the file.

class city {
  final String name;
  final IconData icon;

  const city({
    this.name,
    this.icon,
  });
}

Add this in the state of your widget:

class yourState extend<myWidget>{

List<city> cities = [
    new city(name: "tokio", icon: Icons.location_city),
    new city(name: "paris", icon: Icons.location_city),
    new city(name: "new york", icon: Icons.location_city),
  ];

int index = 0;

Widget drop() {
    return DropdownButton(
        value: cities[index],
        hint: new Text("Ville"),
        items: cities.map((city value) {
          return new DropdownMenuItem(
            value: value,
            child: new Row(
              children: <Widget>[
                new Icon(
                  value.icon,
                  color: Colors.deepOrange,
                ),
                new Text(value.name)
              ],
            ),
          );
        }).toList(),
        onChanged: (city value) {
          setState(() {
            index = cities.indexOf(value);
            print(index);
          });
          //getTravelCity(value.name);
        });
  }

}

For those who are facing this problem in the future. Use a variable to set the value to DropdownButton and when the onChanged called update the variable using setState like the below.

Expanded(child: DropdownButton<String>(
          items: cities.map((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
          value: city,
          onChanged: (String val) {
            _onDropDownChanged(val);
          },
        ),)

Update a city like a below:

void _onDropDownChanged(String val) {
    setState(() {
      this.city = val;
    });
  }

Conclusion:

In this article, we have been through How to Solve DropDown Selection is Not Displaying in Flutter?

Keep Learning !!! Keep Fluttering !!!

Do share your valuable feedback so that we can serve you much better.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply