Files
umbrix/lib/utils/alerts.dart

133 lines
3.2 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:flutter/material.dart';
2023-11-09 16:28:43 +03:30
import 'package:toastification/toastification.dart';
2023-07-06 17:18:41 +03:30
2023-08-19 22:27:23 +03:30
class CustomAlertDialog extends StatelessWidget {
const CustomAlertDialog({
super.key,
this.title,
required this.message,
});
final String? title;
final String message;
2023-08-26 17:01:51 +03:30
factory CustomAlertDialog.fromErr(({String type, String? message}) err) =>
CustomAlertDialog(
title: err.message == null ? null : err.type,
message: err.message ?? err.type,
);
2023-08-19 22:27:23 +03:30
Future<void> show(BuildContext context) async {
await showDialog(
context: context,
2023-10-04 18:06:48 +03:30
useRootNavigator: true,
2023-08-19 22:27:23 +03:30
builder: (context) => this,
);
}
@override
Widget build(BuildContext context) {
final localizations = MaterialLocalizations.of(context);
return AlertDialog(
title: title != null ? Text(title!) : null,
2023-09-06 12:56:30 +03:30
content: SingleChildScrollView(
child: SizedBox(
width: 468,
child: Text(message),
),
),
2023-08-19 22:27:23 +03:30
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(localizations.okButtonLabel),
),
],
);
}
}
2023-11-09 16:28:43 +03:30
enum AlertType {
info,
error,
success;
ToastificationType get _toastificationType => switch (this) {
success => ToastificationType.success,
error => ToastificationType.error,
info => ToastificationType.info,
};
}
2023-07-06 17:18:41 +03:30
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 = Icons.error;
const CustomToast.success(
this.message, {
this.duration = const Duration(seconds: 3),
}) : type = AlertType.success,
icon = Icons.check;
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) {
2023-11-09 16:28:43 +03:30
toastification.show(
context: context,
title: message,
type: type._toastificationType,
alignment: Alignment.bottomLeft,
autoCloseDuration: duration,
style: ToastificationStyle.fillColored,
pauseOnHover: true,
showProgressBar: false,
dragToClose: true,
closeOnClick: true,
closeButtonShowType: CloseButtonShowType.onHover,
2023-07-06 17:18:41 +03:30
);
}
}