refactor: Centralize error and notification handling using a dedicated _errorService across various components.
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// A reusable error display component.
|
||||
///
|
||||
///
|
||||
/// This widget provides a consistent way to display error messages throughout
|
||||
/// the application. It supports customizable titles, messages, icons, and
|
||||
/// action buttons for retry and close operations.
|
||||
class ErrorContent extends StatelessWidget {
|
||||
/// The error title to display
|
||||
final String title;
|
||||
|
||||
|
||||
/// The error message to display
|
||||
final String message;
|
||||
|
||||
|
||||
/// Optional callback for retry action
|
||||
final VoidCallback? onRetry;
|
||||
|
||||
|
||||
/// Optional callback for close action
|
||||
final VoidCallback? onClose;
|
||||
|
||||
|
||||
/// Icon to display with the error
|
||||
final IconData icon;
|
||||
|
||||
|
||||
/// Color of the error icon
|
||||
final Color? iconColor;
|
||||
|
||||
/// Creates a new [ErrorContent] widget.
|
||||
///
|
||||
///
|
||||
/// [message] is required, other parameters are optional with sensible defaults.
|
||||
const ErrorContent({
|
||||
super.key,
|
||||
@@ -79,11 +79,7 @@ class ErrorContent extends StatelessWidget {
|
||||
color: defaultIconColor?.withValues(alpha: 0.1),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 48,
|
||||
color: defaultIconColor,
|
||||
),
|
||||
child: Icon(icon, size: 48, color: defaultIconColor),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
@@ -167,9 +163,7 @@ void showErrorDialog(
|
||||
barrierDismissible: barrierDismissible,
|
||||
builder: (BuildContext dialogContext) {
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
child: ErrorContent(
|
||||
title: title,
|
||||
message: message,
|
||||
@@ -187,70 +181,3 @@ void showErrorDialog(
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Fonction helper pour afficher l'erreur en bottom sheet
|
||||
void showErrorBottomSheet(
|
||||
BuildContext context, {
|
||||
String title = 'Une erreur est survenue',
|
||||
required String message,
|
||||
VoidCallback? onRetry,
|
||||
IconData icon = Icons.error_outline,
|
||||
Color? iconColor,
|
||||
bool isDismissible = true,
|
||||
}) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isDismissible: isDismissible,
|
||||
enableDrag: isDismissible,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
|
||||
),
|
||||
builder: (BuildContext sheetContext) {
|
||||
return SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: ErrorContent(
|
||||
title: title,
|
||||
message: message,
|
||||
icon: icon,
|
||||
iconColor: iconColor,
|
||||
onRetry: onRetry != null
|
||||
? () {
|
||||
Navigator.of(sheetContext).pop();
|
||||
onRetry();
|
||||
}
|
||||
: null,
|
||||
onClose: () => Navigator.of(sheetContext).pop(),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Fonction helper pour afficher en SnackBar (pour erreurs mineures)
|
||||
void showErrorSnackBar(
|
||||
BuildContext context, {
|
||||
required String message,
|
||||
VoidCallback? onRetry,
|
||||
Duration duration = const Duration(seconds: 4),
|
||||
}) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
backgroundColor: Colors.red[400],
|
||||
duration: duration,
|
||||
action: onRetry != null
|
||||
? SnackBarAction(
|
||||
label: 'Réessayer',
|
||||
textColor: Colors.white,
|
||||
onPressed: onRetry,
|
||||
)
|
||||
: null,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user