Flutter DropDown List with Enum

Enum is an essential part of coding. When we need to static dropdown list and get send enum value to the server by using API. Enum creating time we can also create enum string with enum value and send it to API according to need.

What is Enum in Flutter?

An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static, and final (unchangeable - cannot be overridden). we can also access enum value by using Dot (.) in any class or method.


Use of Enum

Mostly enum use for those type data always constant in code like Week,  Month, Gender etc. limited options

This article defines an enum example using dart programming language, with a dropdown list. In this dropdown onChange event, we print enum string and enum value. It example of weekdays.

Before the use of enum, we need to understand how to create an enum in the dart. These steps are given below.

Step 1: Declare A enum and give enum name:-

In this example, we are declaring an enum which name weekEnum in into this enum use weekdays.


enum weekEnum {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

Step 2: Create a String type method for enum name

String weeksName(weekEnum deptRepair) {
switch (deptRepair) {
case weekEnum.Monday:
return "Monday";
case weekEnum.Tuesday:
return "Tuesday";
case weekEnum.Wednesday:
return "Wednesday";
case weekEnum.Thursday:
return "Thursday";
case weekEnum.Friday:
return "Friday";
case weekEnum.Saturday:
return "Saturday";
case weekEnum.Sunday:
return "Sunday";
}
}

Step 3: Create another Integer method for enum values
int weekEnumValue(weekEnum weekName) {
switch (weekName) {
case weekEnum.Monday:
return 1;
case weekEnum.Tuesday:
return 2;
case weekEnum.Wednesday:
return 3;
case weekEnum.Thursday:
return 4;
case weekEnum.Friday:
return 5;
case weekEnum.Saturday:
return 6;
case weekEnum.Sunday:
return 7;
}
}



Source code of the above example image:-

Previous
Next Post »