forked from hiddify/hiddify-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.dart
133 lines (118 loc) · 3.31 KB
/
alerts.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:toastification/toastification.dart';
class CustomAlertDialog extends StatelessWidget {
const CustomAlertDialog({
super.key,
this.title,
required this.message,
});
final String? title;
final String message;
factory CustomAlertDialog.fromErr(({String type, String? message}) err) =>
CustomAlertDialog(
title: err.message == null ? null : err.type,
message: err.message ?? err.type,
);
Future<void> show(BuildContext context) async {
await showDialog(
context: context,
useRootNavigator: true,
builder: (context) => this,
);
}
@override
Widget build(BuildContext context) {
final localizations = MaterialLocalizations.of(context);
return AlertDialog(
title: title != null ? Text(title!) : null,
content: SingleChildScrollView(
child: SizedBox(
width: 468,
child: Text(message),
),
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(localizations.okButtonLabel),
),
],
);
}
}
enum AlertType {
info,
error,
success;
ToastificationType get _toastificationType => switch (this) {
success => ToastificationType.success,
error => ToastificationType.error,
info => ToastificationType.info,
};
}
class CustomToast extends StatelessWidget {
const CustomToast(
this.message, {
this.type = AlertType.info,
this.icon,
this.duration = const Duration(seconds: 3),
});
const CustomToast.error(
this.message, {
this.duration = const Duration(seconds: 5),
}) : type = AlertType.error,
icon = FluentIcons.error_circle_24_regular;
const CustomToast.success(
this.message, {
this.duration = const Duration(seconds: 3),
}) : type = AlertType.success,
icon = FluentIcons.checkmark_24_regular;
final String message;
final AlertType type;
final IconData? icon;
final Duration duration;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final color = switch (type) {
AlertType.info => null,
AlertType.error => scheme.error,
AlertType.success => scheme.tertiary,
};
return Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(4)),
color: Theme.of(context).colorScheme.surface,
),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (icon != null) ...[
Icon(icon, color: color),
const SizedBox(width: 8),
],
Flexible(child: Text(message)),
],
),
);
}
void show(BuildContext context) {
toastification.show(
context: context,
title: Text(message),
type: type._toastificationType,
alignment: Alignment.bottomLeft,
autoCloseDuration: duration,
style: ToastificationStyle.fillColored,
pauseOnHover: true,
showProgressBar: false,
dragToClose: true,
closeOnClick: true,
closeButtonShowType: CloseButtonShowType.onHover,
);
}
}