AlertDialog in Flutter | Flutter Alert Dialog Example:
it is used to display message box into flutter application.
AlertDialog(title:Text("content")
................................................
Type of Alert Dialog
1) Basic AlertDialog
2) Confirmation Dialog
3) Select AlertDialog
4) TextField AlertDialog
Example of BasicAlertDialog:
import 'package:flutter/material.dart';
class MyAlertDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
void _showAlertDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Alert"),
content: Text("This is an alert dialog in Flutter."),
actions: <Widget>[
TextButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Flutter AlertDialog Example")),
body: Center(
child: ElevatedButton(
child: Text("Show Alert"),
onPressed: () {
_showAlertDialog(context);
},
),
),
);
}
}
Example of Other Dialog:
import 'package:flutter/material.dart';
class Confirmationdialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SecondScreen(),
);
}
}
class SecondScreen extends StatelessWidget {
void _showInputDialog(BuildContext context) {
TextEditingController _controller = TextEditingController();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Enter Input"),
content: TextField(
controller: _controller,
decoration: InputDecoration(hintText: "Type something..."),
),
actions: <Widget>[
TextButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
),
TextButton(
child: Text("Submit"),
onPressed: () {
String inputText = _controller.text;
Navigator.of(context).pop(); // Close the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("You entered: $inputText")),
);
},
),
],
);
},
);
}
void _showSelectDialog(BuildContext context) {
List<String> options = ["Option 1", "Option 2", "Option 3"];
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Select an Option"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: options.map((option) {
return ListTile(
title: Text(option),
onTap: () {
Navigator.of(context).pop(); // Close the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("You selected: $option")),
);
},
);
}).toList(),
),
);
},
);
}
void _showAlertDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Alert"),
content: Text("This is an alert dialog in Flutter."),
actions: <Widget>[
TextButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
),
TextButton(
child: Text("Confirm"),
onPressed: () {
// Handle the confirmation action
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Confirmed!")),
);
},
)
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Flutter AlertDialog Example")),
body: Center(
child: ElevatedButton(
child: Text("Show Confirm Alert"),
onPressed: () {
_showInputDialog(context);
},
),
),
);
}
}
POST Answer of Questions and ASK to Doubt