AlertDialog Sample — Flutter
In the previous story, we have learned to create a menu sample in flutter. In this story, we can learn to create Alert Dialog sample in flutter.
If you are a native Android developer, you might get surprised because there is no negative, positive button here. Instead of those, we can add any number of buttons and actions.
ElevatedButton
We have already used this button in one of our previous stories. Here, we used this elevated button to show the alert dialog. In the onPressed callback of the elevated button, we are calling a method showAlertDialog(context).
showAlertDialog
This is a custom method we used to hold the buttons, button’s actions and the alert dialog.
TextButton
We are using TextButton to show the buttons in the alert dialog. And closing the alert dialog once onPressed callback is received using the Navigator pop method.
AlertDialog
Alert Dialog consists of title, content and actions. As the name suggests, the title is to show the title and the content is to show content. The actions can hold any number of actions, which adds the buttons to the Alert Dialog.
Yes, it is that simple to create an alert dialog in the flutter. You can find the full code in the above-mentioned code pen link. Happy coding!