main.dart
import 'package:flutter/material.dart';
void main() => runApp(const FlutterExample());
class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.pink),
home: const StateExample()
);
}
}
class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);
@override
_StateExampleState createState() => _StateExampleState();
}
class _StateExampleState extends State<StateExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: AppBar(
title: const Text("Flutter - AlertDialog Example")
),
body: bodyContent()
);
}
bodyContent(){
return Center(
child: ElevatedButton(
child: const Text("Show Dialog"),
onPressed: (){
showAlertDialog();
},
),
);
}
showAlertDialog(){
Widget cancelButton = TextButton(
onPressed: (){
Navigator.pop(context);
SnackBar snackBar = const SnackBar(
content: Text("Cancel Clicked.")
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: const Text("Cancel")
);
Widget positiveButton = TextButton(
onPressed: (){
Navigator.pop(context);
SnackBar snackBar = const SnackBar(
content: Text("Positive button clicked.")
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: const Text("Yes")
);
AlertDialog dialog = AlertDialog(
title: const Text("Dialog Title"),
content: const Text("This is alert dialog Content."),
actions: [
cancelButton,
positiveButton
],
);
showDialog(
context: context,
builder: (BuildContext context){
return dialog;
});
}
}
![](https://fluttertuts.wordpress.com/wp-content/uploads/2021/07/7a6f2-flutteralertdialogexample.png)
![](https://fluttertuts.wordpress.com/wp-content/uploads/2021/07/a74a5-flutteralertdialogexample2.png)
More flutter tutorials
- flutter – PageView builder
- flutter – AlertDialog background color
- flutter – AlertDialog width
- flutter – AlertDialog height
- flutter – Transparent status bar
- flutter – How to show hide status bar
- flutter – AppBar center title
- flutter – How to remove AppBar drop shadow
- flutter – How to change AppBar color
- flutter – MaterialApp theme
- flutter – Container padding and margin
- flutter – Container full width
- flutter – How to align Text
- flutter – Space between Text and underline
- flutter – How to add shadow to the Text