DEV Community

Cover image for How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter - fluttercorner.com
FlutterCorner
FlutterCorner

Posted on

2

How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter - fluttercorner.com

Hello Guys How Are You all ? In This Tutorial We are going to learn How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter.

Many Times We need to give users to option or dropdown button to choose one option. So there we need to use dropdown button. For Example Choose Gender Button.

To make Dropdown Button we need to use DropdownButton Class. in this class we need to provide some value to key items. So Without Wasting Your Time Lets Start this Tutorial.

How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter?

First of All Import material.dart in your main.dart file.

import 'package:flutter/material.dart';
Enter fullscreen mode Exit fullscreen mode

Then, Create void main and Define MyApp in your runApp.

void main() {
  runApp(MyApp());
}
Enter fullscreen mode Exit fullscreen mode

Now, Create a class named MyApp extends with a Stateless widget. This is our main View class. and define

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "DropDown Button Example - FlutterCorner",
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("DropDown Button Example - FlutterCorner"),
          backgroundColor: Colors.black,
        ),
        body: DropDownList(),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Then, Make DropDownList StatefulWidget Class.

class DropDownList extends StatefulWidget {
  @override
  _DropDownListState createState() => _DropDownListState();
}

class _DropDownListState extends State<DropDownList> {

}
Enter fullscreen mode Exit fullscreen mode

After That, let’s make a String variable with Default Value like below.

  String dropdownValue = 'First';
Enter fullscreen mode Exit fullscreen mode

Now, Make DropdownButton class with key named value, onChanged and items.

Here value would be our final value that user choosen
onChanged will have setState. when user choose any item from dropdown it will assign final value.

items have a list of item that we want to open as dropdown.

       DropdownButton<String>(
            value: dropdownValue,
            onChanged: (String newValue) {
              setState(() {
                dropdownValue = newValue;
              });
            },
            items: <String>['First', 'Second', 'Third', 'Fourth']
                .map<DropdownMenuItem<String>>((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
          )
Enter fullscreen mode Exit fullscreen mode

The result will be the following: ( I am used Raised Button So Your Result Would be Minor Different from my result ).

Full Article At Here How to Create Dropdown Button In Flutter, Dropdown Lists in Flutter

Neon image

Resources for building AI applications with Neon Postgres 🤖

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools →

Top comments (2)

Collapse
 
derrickthesmart profile image
Derrick Koko💻

Thanks...
The code worked perfectly...

Collapse
 
kwame_yeboah profile image
kwame_yeboah

Thanks your code helped notice an error in mine :)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay