Home > database >  Dismiss AlertDialog after showing up for several seconds without do action button in Flutter
Dismiss AlertDialog after showing up for several seconds without do action button in Flutter

Time:11-27

I have an AlertDialog popped up just for several seconds and then automatically popped out without do any action buttons. After it popped out, it will be shows another AlertDialog.

How i can do that?

This is my example code:

Initialize Timer For Show Up The First Dialog:

_timerToShowFirstDialog() async {
  var duration = const Duration(seconds: 1);
  return Timer(duration, () {
    _showFirstDialog();
 });
}

First Dialog:

_showFirstDialog(){
 return showDialog<String>(
 context: context,
 barrierDismissible: false,
 builder: (BuildContext context) {
   return AlertDialog(
     title: Text('Show Dialog 1'),
     content: Text('Wait a sec'),
   );
  });
}

Second Dialog:

_showSecondDialog(){
 return showDialog<String>(
 context: context,
 barrierDismissible: false,
 builder: (BuildContext context) {
   return AlertDialog(
     title: Text('Show Dialog 2'),
     content: Text('After the first dialog popped up for several seconds, shows this dialog without any action buttons'),
   );
  });
}

I was thinking to using setState in the First Dialog to call/return the second one. But, i don't have any idea where to put the code is.

I appreciate for any answers

CodePudding user response:

Try below code hope its help to you. just tapped on button first then alert is open first alert closes to 5 sec then open 2nd alert

refer Future.delayed() here

 TextButton(
  onPressed: () => showDialog(
      context: context,
      builder: (BuildContext context) {
        Future.delayed(Duration(seconds: 5), () {
          Navigator.of(context).pop(true);
        });
        return AlertDialog(
          title: const Text('AlertDialog Title'),
          content: const Text('AlertDialog description'),
        );
      }).then((value) {
    showDialog(
      // Second dialog open
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Center(child: Text('Welcome')),
              Text('Second Dialog')
            ],
          ),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.pop(context, 'Cancel'),
              child: const Text('Cancel'),
            ),
            TextButton(
              onPressed: () => Navigator.pop(context, 'OK'),
              child: const Text('OK'),
            ),
          ],
        );
      },
    );
  }),
  child: const Text('Show Dialog'),
)
  • Related