How to Show an Alert Dialog Box in Flutter?

Achintha Isuru
2 min readOct 9, 2019

This is my second article about flutter framework. Before diving into coding let’s get to know about when to use Alert Dialog boxes. They are mainly used to get confirmation from the user, for instance, when the user wants to logout from your app etc.

Now let’s dive into the coding!

Coding | Creating the Interface

Today I am going to create an interface with a button when it is clicked an Alert Dialog will be shown.

Before creating the Alert Dialog Box let’s create our interface.

I will not explain any implementation details today, because the main intention of this article is learning about the Alert Dialog box.

Coding | Showing the Alert Dialog

Now we have created our interface, now let’s show an Alert Dialog Box!

As I told earlier, the button click will show the Alert Dialog Box. Therefore we have to write a method (or a function) to show the Alert Dialog Box.

Now we have written the method(or function) to show the Alert Dialog Box. Now we pass it to the onClick variable in the RaisedButton.

Now you have a working Alert Dialog Box.

Important things to Remember

The main thing to remember is that we have to pass the correct context for the method (The passed context must be the context of a Scaffold Widget).

If not the app may not work properly.

Github link to the code shown in the article

You can find the code shown in the article, in this GitHub link

Conclusion

This is the end of the article, and before I finish this article I want to say, if I had made any mistakes or if there is an easy way to do this please add a comment below about it.

Thank you and HAPPY CODING!

--

--

No responses yet