Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Create a Single Selected Dropdown List in Flutter?

I am a flutter beginner. How to Create a Single Selected Dropdown List in Flutter?
Like This.
I tried, but I didn’t get what I wanted.

String dropdownvalue = ‘Bahrain’;

            Container(
              width: 308,
              child: DropdownButton(
                // Initial Value
                value: dropdownvalue,

                // Down Arrow Icon
                icon: const Icon(Icons.keyboard_arrow_down),

                // Array list of items
                items: dropdownvalue.map((String dropdownvalue) {
                  return DropdownMenuItem(
                    value: dropdownvalue,
                    child: Text(dropdownvalue),
                  );
                }).toList(),
                // After selecting the desired option,it will
                // change button value to selected value
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownvalue = newValue!;
                  });
                },
              ),
            ),

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should have list of items and selectedItem to manage the list and selection state.

class _YourState extends State<MyHomePage> {
  List<String> countries = [
    'Bahrain',
    'India',
    'Iraq',
    'America',
  ];

  String? dropdownvalue ;

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body: Center(
        child: DropdownButton(
          // Initial Value
          value: dropdownvalue,
          icon: const Icon(Icons.keyboard_arrow_down),
          isExpanded: true,
          items: countries.map((String dropdownvalue) {
            return DropdownMenuItem(
              value: dropdownvalue,
              child: Text(dropdownvalue),
            );
          }).toList(),
          hint: Text('Country'),
          onChanged: (String? newValue) {
            setState(() {
              dropdownvalue = newValue!;
            });
          },
        ),
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading