How to use AlertDialog and Dismiss Alert

Does you application needs to alert your user or maybe to get input from them? Try using Flutter AlertDialog widget and dismiss alert dialog with an example.

How to use AlertDialog and Dismiss Alert
How to use AlertDialog and Dismiss Alert - mobilelabs.in

Does you application needs to alert your user or maybe to get input from them?

Try using Flutter AlertDialog widget.

Here is an example how to use Flutter AlertDialog widget in your project

Future<void> logOut() async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Are you sure?'),
        content: SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              Text('We will be redirected to login page.'),
            ],
          ),
        ),
        actions: <Widget>[
	FlatButton(
		child: Text('No'),
		onPressed: () {
		  Navigator.of(context).pop(); // Dismiss the Dialog
		},
	 ),
	 FlatButton(
		child: Text('Yes'),
		onPressed: () {
		  Navigator.of(context).pop(); // Navigate to login
		},
	  ),
        ],
      );
    },
  );
}
Usually content is a Text widget but you can also use Image, SingleChildScrollView, etc.

How to dismiss Alert dialog in Flutter

You can dismiss alert dialog by Navigator.pop(context); or you can also use

Navigator.pop(context, result);
Dismiss alert dialog in flutter and passing result
FlatButton(
  child: Text('No'),
  onPressed: () {
	Navigator.of(context).pop(); // Dismiss alert dialog in flutter
  },
)
Dismiss alert dialog in flutter on button click

See also: