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