Skip to content

Commit 117987c

Browse files
authored
Create reusable-alert-dialogs-in-flutter.dart
1 parent 3e8c532 commit 117987c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 🐦 Twitter https://twitter.com/vandadnp
2+
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
3+
// 🎥 YouTube https://youtube.com/c/vandadnp
4+
// 💙 Free Flutter Course https://linktr.ee/vandadnp
5+
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
6+
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
7+
// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
8+
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
9+
10+
import 'package:flutter/material.dart';
11+
12+
@immutable
13+
class AlertDialogModel<T> {
14+
final String title;
15+
final String message;
16+
final Map<String, T> buttons;
17+
18+
const AlertDialogModel({
19+
required this.title,
20+
required this.message,
21+
required this.buttons,
22+
});
23+
}
24+
25+
@immutable
26+
class DeleteDialog extends AlertDialogModel<bool> {
27+
const DeleteDialog({required String objName})
28+
: super(
29+
title: 'Delete $objName?',
30+
message: 'Are you sure you want to delete this $objName?',
31+
buttons: const {
32+
'CANCEL': false,
33+
'DELETE': true,
34+
},
35+
);
36+
}
37+
38+
Future<bool> displayDeleteDialog(BuildContext context) =>
39+
const DeleteDialog(objName: 'comment').present(context).then(
40+
(value) => value ?? false,
41+
);
42+
43+
extension Present<T> on AlertDialogModel<T> {
44+
Future<T?> present(BuildContext context) {
45+
return showDialog<T?>(
46+
context: context,
47+
builder: (context) {
48+
return AlertDialog(
49+
title: Text(title),
50+
content: Text(message),
51+
actions: buttons.entries.map(
52+
(entry) {
53+
return TextButton(
54+
child: Text(
55+
entry.key,
56+
),
57+
onPressed: () {
58+
Navigator.of(context).pop(
59+
entry.value,
60+
);
61+
},
62+
);
63+
},
64+
).toList(),
65+
);
66+
},
67+
);
68+
}
69+
}

0 commit comments

Comments
 (0)