class DemoAlertDialogWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
void visibleShowSnackbar(String text, String ans) {
final snackBar = SnackBar(
duration: Duration(milliseconds: 500),
backgroundColor:
ans.compareTo("Yes") == 0 ? Colors.purple : Colors.amber,
content: Row(
children: <Widget>[
Icon(
ans.compareTo("Yes") == 0 ? Icons.ac_unit_sharp : Icons.account_balance_sharp,
color: ans.compareTo("Yes") == 0 ? Colors.blue : Colors.black,
size: 26.0,
semanticLabel: text,
),
Text(text)
],
));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
// First Normal Dialog Show
Future<void> _normalShowingSimpleDialog() async {
switch (await showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('First Normal Showing Dialog, Do you want to close it?'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, "Yes");
},
child: const Text('Yes'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, "No");
},
child: const Text('No'),
),
],
);
})) {
case "Yes":
visibleShowSnackbar("Thanks!", "Yes");
break;
case "No":
visibleShowSnackbar("Ohk! Hope you are doing well", "No");
break;
}
}
Future<void> _alertDialog() async {
switch (await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Do you want to close it??'),
title: Text('Alert Dialog'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context, "Yes");
},
child: const Text('Yes'),
),
TextButton(
onPressed: () {
Navigator.pop(context, "No");
},
child: const Text('No'),
),
],
);
})) {
case "Yes":
visibleShowSnackbar("Thanks!", "Yes");
break;
case "No":
visibleShowSnackbar("Ohk! Hope you are doing well", "No");
break;
}
}
void _timerDialog() {
DateTime now = DateTime.now();
showTimePicker(
context: context,
initialTime: TimeOfDay(hour: now.hour, minute: now.minute))
.then((onValue) {
visibleShowSnackbar(onValue.format(context), "ok");
});
}
void _datePickerDialog() {
DateTime now = DateTime.now();
showDatePicker(
context: context,
initialDate: now,
firstDate: DateTime(2000),
lastDate: DateTime(2050))
.then((onValue) {
visibleShowSnackbar('$onValue', "ok");
});
}
return Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
child: Text('First Normal Dialog'),
onPressed: _normalShowingSimpleDialog,
),
ElevatedButton(
child: Text('The Alert Dialog'),
onPressed: _alertDialog,
),
ElevatedButton(
child: Text('The Date Picker Dialog'),
onPressed: _datePickerDialog,
),
ElevatedButton(
child: Text('The Timer Picker Dialog'),
onPressed: _timerDialog,
),
],
),
),
);
}
}
Be the first to comment.