Flutter - Implement DropdownButton Inside an AlertDialog
Last Updated :
28 Apr, 2025
DropdownButton widget is used to create a dropdown menu that allows users to select one option from a list of available choices. It's a common user interface element for selecting items from a list. An Alert Dialog is a useful way to grab users' attention. Here we can see how to implement an AlertDialog, and then we are going to implement a DropdownButton inside the AlertDialog. In this article, we will implement the AertDialog and DropdownButton, A sample video is given below to get an idea about what we are going to do in this article.
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementations
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
home: DropDownAlertDialog(),
);
}
}
Step 5: Create DropDownAlertDialog Class
In this class we are going to Implement the DropdownButton widget inside an Alert Dialog that help to create a dropdown menu where users can select one option from a list of available options and the selected option is displayed in the debug console window .Comments are added for better understanding.
// Method for implementing an DropDownButton inside an AlertDialog
void showMyDialog(BuildContext context) {
// Creating an Dialog
showDialog(
context: context,
builder: (BuildContext context) {
// Use a nullable type
String? selectedItem = 'Option 1';
// Creating a list of avaliable options
List<String> items = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];
return AlertDialog(
title: Text('Select an option'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButton<String>(
value: selectedItem,
items: items.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
onChanged: (String? newValue) {
// Use a nullable type for onChanged
if (newValue != null) {
selectedItem = newValue;
}
},
),
],
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Handle the selected item here
if (selectedItem != null) {
print('Selected item: $selectedItem');
}
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
Dart
class DropDownAlertDialog extends StatelessWidget {
const DropDownAlertDialog({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dropdown in AlertDialog'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showMyDialog(context);
},
child: Text('Show AlertDialog'),
),
),
);
}
//Method for implementing an DropDownButton inside an AlertDialog
void showMyDialog(BuildContext context) {
//Creating an Dialog
showDialog(
context: context,
builder: (BuildContext context) {
// Use a nullable type
String? selectedItem = 'Option 1';
// Creating a list of avaliable options
List<String> items = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];
return AlertDialog(
title: Text('Select an option'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButton<String>(
value: selectedItem,
items: items.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
onChanged: (String? newValue) {
// Use a nullable type for onChanged
if (newValue != null) {
selectedItem = newValue;
}
},
),
],
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Handle the selected item here
if (selectedItem != null) {
print('Selected item: $selectedItem');
}
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
// Set the app's primary theme color
primarySwatch: Colors.green,
),
home: DropDownAlertDialog(),
);
}
}
class DropDownAlertDialog extends StatelessWidget {
const DropDownAlertDialog({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dropdown in AlertDialog'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showMyDialog(context);
},
child: Text('Show AlertDialog'),
),
),
);
}
// Method for implementing an
// DropDownButton inside an AlertDialog
void showMyDialog(BuildContext context) {
// Creating an Dialog
showDialog(
context: context,
builder: (BuildContext context) {
// Use a nullable type
String? selectedItem = 'Option 1';
// Creating a list of avaliable options
List<String> items = ['Option 1', 'Option 2', 'Option 3', 'Option 4'];
return AlertDialog(
title: Text('Select an option'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButton<String>(
value: selectedItem,
items: items.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
onChanged: (String? newValue) {
// Use a nullable type for onChanged
if (newValue != null) {
selectedItem = newValue;
}
},
),
],
),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Handle the selected item here
if (selectedItem != null) {
print('Selected item: $selectedItem');
}
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
}
Output:
Similar Reads
Flutter - Implement an Image Inside an AlertDialog
AlertDialogs are a common tool used to deliver messages, notifications, or interactive content to users. While Flutter provides a straightforward way to create AlertDialogs, it may not be immediately obvious how to include images inside them. Fortunately, this article will guide you through the proc
4 min read
Flutter - Implement Stepper Widget Inside an AlertDialog
The Stepper widget in Flutter is a user interface element used to create a step-by-step process interface. It allows users to progress through a series of steps or stages, with each step typically representing a specific task or some information. An Alert Dialog is a useful way to grab users' attent
5 min read
Flutter - DropDownButton Widget
In this article, we will learn how to use a DropDownButton and learn various properties of it in flutter. We will use the Flutter DropDownButton widget to display a dropdown list in our application. So first letâs see what is DropDownButton.DropDownButton Widget FlutterIn Flutter, A DropDownButton i
3 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this
4 min read
Flutter - Implement Status Alert
In Flutter, a "status alert" typically refers to a visual feedback mechanism that informs the user about the status or outcome of a certain operation or event. The status_alert package in Flutter is a third-party package that provides an easy way to create and display such status alerts. These alert
4 min read
Flutter - Remove Arrow Button in the AppBar
We can remove the Arrow button in the AppBar by various methods, but in this article, we will cover one of the following methods. Basically this arrow appears when we navigate to the new screen so that we can get back to the previous screen. But if we don't want this arrow button, then we can use th
4 min read
Flutter - Display a GridView Within an AlertDialog Box
AlertDialogs are useful Widgets in Flutter to attract the users attention to a particular message that is showing by the Alert Dialogs, GridView are efficient widgets to display items in a Grid like structure. To implement a GridView inside an AlertDialog in Flutter, you can create a custom dialog c
5 min read
How to Implement Circular Animated Floating Action Button in Flutter?
The Floating Action Button is the circular button that floats on the bottom of the  Application screen, It may perform many tasks like showing the menu bar, capturing images, opening the gallery anything you want. In this article, we are creating the Circular Animated Floating Action Button. Step By
3 min read
Flutter - Implementing Swipe to Dismiss Feature
The Swipe to dismiss feature is used by us in many mobile apps. In this article, we will create a list of items and implement a swipe to dismiss in it. When we want to dismiss the content using swipe then we can do this with the help of swipe to dismiss widget in a flutter. Generally, we use this wh
2 min read
Flutter - Implementing Badges
Badges can be used for various purposes in an application. For example, showing the number of messages, number of items in the cart, etc. In this article, we will see the implementation of badges in Flutter using the badges Flutter package. Install the library: In the pubspec.yaml file, add badges l
5 min read