In this tutorial, you will learn how to use the DropdownButton in the flutter.

What we will learn in this tutorial?

Before Start

Before starting our tutorial, I will create a page to contain our code.

It will show an empty page and I will put the code in the children .

Simple Use

So let’s look at our first simple about the DropDownButton .

When we click the button, it will show you in the picture as below. It is very useful to our

Constructor

As common, Let’s look at the constructor of the DropdownButton .

So let’s look at the parameters step by step.

items

We have used this parameter above, but we don’t really know it. When we see its definition, we know it is a list.

Then we look at the constructor of the DropdownMenuItem .

You need to know that the T is a common class, usually, it is the type of your data. Such as String , double . The value will be this class same as the T . The child will show in the menu. It is a widget, I use a Text in the fore example. In this example, I will use a Row to show you.

It will show you as follows. You should notice isExpanded == true . It will expand the menu. When you click it, it will show you the right one.

But you should care about that the _value should be initiated with a valid value. In the above code, we have two choices, 1 , 2 . var _value = "1"; If you set it to other value, such _value ="3" .It will occur such error.

hint&disabledHint

If you do not set value, it will show a widget in the default state. Let’s look at the code with not setting the value parameter.

It will show you like this.

If we set onChange or items to null, I will show the disabledHint .

style

In this part, I will give an example of many parameters, which are very easy to understand. These parameters are below.

So let’s look at the example.

The elevation controls the shadow of the menu, we have used it so many time, so I will not tell this again in the other widgets. The style controls the Text in the menu. The isDense will reduce the height of the button. The iconSize controls the size of the triangle-arrow. The result is below.

Conclusion

Today, we have learned the DropdownButton . It just jumps out a menu to select. It is easy to understand, but need care about the value must be in the range of values of the items.

Thanks for your reading.

The End.

The whole code in Github: https://github.com/FlutterOpen/ebook