Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make alertDialog disappear automatically after few seconds in Flutter?

Tags:

An alertDialog is to be shown when a button is tapped and disappear automatically after few seconds. How can I do that in Flutter?

like image 735
Naveen Avatar asked Dec 04 '18 14:12

Naveen


People also ask

How do you dismiss dialog automatically in Flutter?

To Dismiss Dialog user needs to make use of an inbuilt class like showDialog. The dialog route created by this method is pushed to the root navigator. If the application has multiple Navigator objects. It may be necessary to call Navigator.

How do I know if AlertDialog is showing in Flutter?

You can do it using showGeneralDialog and put dismissible property to false , using this will ensure you that you are the only one who is handling pop up, like in your any action buttons in dialog.

How does AlertDialog return value in Flutter?

You can access and use the value that comes from your dialog option like this: showDialog( context: context, builder: (context) => Dialog( val: vale, ), ). then((valueFromDialog){ // use the value as you wish print(valueFromDialog); });


2 Answers

A Minimal E.g:

It Closes the alertDialog after 5 seconds.

           showDialog(                       context: context,                       builder: (context) {                         Future.delayed(Duration(seconds: 5), () {                           Navigator.of(context).pop(true);                         });                         return AlertDialog(                           title: Text('Title'),                         );                       }); 
like image 124
anmol.majhail Avatar answered Sep 28 '22 00:09

anmol.majhail


Future.delayed can cause some problems if you dismiss the dialog before the Future is triggered. So, if you use it be careful that the showDialog is not dismissable barrierDismissible: false and that the AlertDialog hasn't buttons that dismiss it.

Otherwise, you can use a Timer:

Timer timer = Timer(Duration(milliseconds: 3000), (){   Navigator.of(context, rootNavigator: true).pop(); }); showDialog(   ... Dialog Code ... ).then((value){   // dispose the timer in case something else has triggered the dismiss.   timer?.cancel();   timer = null; }); 
like image 42
Apoleo Avatar answered Sep 28 '22 00:09

Apoleo