Creating dialogs in Flutter

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Dialogs are an important part of applications, and so in this article, we'll learn how to create dialogs in Flutter.
We'll be learning how to create a dialog in Flutter and how which options we can use.
The end result will be a dialog like this:

I'll use my basic flutter app as the starting point if you want to code along.
Let's add a simple button to our Flutter application. When we press this button, we want the dialog to show.
Open up your main.dart file and add the following code to get the button we want.
return Center(
child: ElevatedButton(
child: Text("Click me for a dialog"),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Success"),
content: Text("Saved successfully"),
);
},
);
},
),
);
This will give us a primary button, and once we click that, we get a dialog with some text in it, we can then click anywhere on the screen to close this dialog.
However, sometimes we want to use some buttons on a dialog screen to capture specific actions.
We can do this by passing the attributes of the action, which can hold multiple buttons.
child: ElevatedButton(
child: Text("Click me for a dialog"),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Do you agree?"),
content: Text("With this ridiculous statement."),
actions: [
ElevatedButton(onPressed: () {}, child: Text("Disagree")),
ElevatedButton(onPressed: () {}, child: Text("Agree")),
],
);
},
);
},
),
This will give us a dialog with two buttons, we can add actions to these buttons as we please, for instance just close the dialog:
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Disagree"),
),
But you can add your custom actions there as well.
Another cool part about Flutter in general, including this dialog is that we can style it to our own needs!
For instance we could style the dialog to be very colourful:
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.purpleAccent,
title: Text(
"Do you agree?",
style: TextStyle(fontSize: 20, color: Colors.white),
),
content: Text(
"With this ridiculous statement.",
style: TextStyle(color: Colors.white),
),
actions: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.redAccent,
),
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Disagree"),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.greenAccent,
),
onPressed: () {},
child: Text("Agree")),
],
);
},
);
This is, of course, a pretty eccentric example, but as you can see, almost every element of the dialog can be custom styled. It's cool that we get so much freedom in styling elements in Flutter.
With this, I want to end the article. I've uploaded the most extended example to GitHub to see the complete code.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter