import 'package:flutter/material.dart'; import '../components/error/error_content.dart'; /// Service for handling application errors and user notifications. /// /// This singleton service provides centralized error handling capabilities /// including displaying error dialogs, snackbars, and logging errors for /// debugging purposes. It uses a global navigator key to show notifications /// from anywhere in the application. class ErrorService { static final ErrorService _instance = ErrorService._internal(); /// Factory constructor that returns the singleton instance. factory ErrorService() => _instance; /// Private constructor for singleton pattern. ErrorService._internal(); /// Global navigator key for accessing context from anywhere in the app. /// /// This key should be assigned to the MaterialApp's navigatorKey property /// to enable error notifications from any part of the application. static GlobalKey navigatorKey = GlobalKey(); /// Displays an error message in a dialog. /// /// Shows a modal dialog with the error message and optional retry functionality. /// The dialog appearance can be customized with different icons and colors. /// /// [message] - The error message to display /// [title] - The dialog title (defaults to 'Error') /// [onRetry] - Optional callback for retry functionality /// [icon] - Icon to display in the dialog /// [iconColor] - Color of the icon void showError({ required String message, String title = 'Error', VoidCallback? onRetry, IconData icon = Icons.error_outline, Color? iconColor, }) { final context = navigatorKey.currentContext; if (context != null) { showErrorDialog( context, title: title, message: message, icon: icon, iconColor: iconColor, onRetry: onRetry, ); } } /// Displays an error or success message in a snackbar. /// /// Shows a floating snackbar at the bottom of the screen with the message. /// The appearance changes based on whether it's an error or success message. /// /// [message] - The message to display /// [onRetry] - Optional callback for retry functionality /// [isError] - Whether this is an error (true) or success (false) message void showSnackbar({ required String message, VoidCallback? onRetry, bool isError = true, }) { final context = navigatorKey.currentContext; if (context != null) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(message), backgroundColor: isError ? Colors.red[400] : Colors.green[600], duration: const Duration(seconds: 4), action: onRetry != null ? SnackBarAction( label: 'Retry', textColor: Colors.white, onPressed: onRetry, ) : null, behavior: SnackBarBehavior.floating, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), ); } } /// Logs error messages to the console during development. /// /// Formats and displays error information including source, error message, /// and optional stack trace in a visually distinct format. /// /// [source] - The source or location where the error occurred /// [error] - The error object or message /// [stackTrace] - Optional stack trace for debugging void logError(String source, dynamic error, [StackTrace? stackTrace]) { print('═══════════════════════════════════'); print('❌ ERROR in $source'); print('Message: $error'); if (stackTrace != null) { print('StackTrace: $stackTrace'); } print('═══════════════════════════════════'); } /// Logs informational messages to the console during development. /// /// [source] - The source or location of the information /// [message] - The informational message void logInfo(String source, String message) { print('ℹ️ [$source] $message'); } /// Logs success messages to the console during development. /// /// [source] - The source or location of the success /// [message] - The success message void logSuccess(String source, String message) { print('✅ [$source] $message'); } }