Flutter Notification System đź””

Una librerĂ­a completa de gestiĂłn de notificaciones para Flutter que sigue los principios de Clean Architecture.

✨ Características

  • 🎯 Type-Safe: Sistema completamente tipado con Dart
  • 🏗️ Clean Architecture: SeparaciĂłn clara de responsabilidades
  • 📊 Queue Management: Cola de notificaciones con sistema de prioridades
  • 🎨 Personalizable: Temas, colores, animaciones y builders personalizados
  • 🔄 Operation Tracking: Mixin para rastrear resultados de operaciones CRUD
  • đź§Ş Testeable: Cobertura completa de tests unitarios e integraciĂłn
  • ⚡ Performance: Optimizado con Selector y Mixin patterns
  • đź”” MĂşltiples tipos: Success, Error, Warning, Info
  • 📱 Responsive: SnackBars y Dialogs adaptativos
  • 🎭 Animaciones: MĂşltiples efectos de animaciĂłn configurables

📦 Instalación

Agrega esto a tu pubspec.yaml:

dependencies:
  flutter_notification_system: ^1.0.0

Luego ejecuta:

flutter pub get

🚀 Uso Rápido

1. ConfiguraciĂłn Inicial

import 'package:flutter_notification_system/flutter_notification_system.dart';
import 'package:get_it/get_it.dart';

final getIt = GetIt.instance;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Registrar mĂłdulo de notificaciones
  registerNotificationModule(getIt);
  
  runApp(MyApp());
}

2. Configurar en App Root

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<NotificationViewModel>.value(
      value: getIt<NotificationViewModel>(),
      child: Consumer<NotificationViewModel>(
        builder: (context, viewModel, _) {
          viewModel.setAppContext(context);
          
          return AppNotificationListener(
            child: MaterialApp(
              title: 'Mi App',
              home: HomePage(),
            ),
          );
        },
      ),
    );
  }
}

3. Mostrar Notificaciones

// Notificación de éxito
getIt<NotificationViewModel>().showSuccess(
  message: 'OperaciĂłn completada exitosamente',
);

// NotificaciĂłn de informaciĂłn
getIt<NotificationViewModel>().showInfo(
  message: 'Datos actualizados',
);

// NotificaciĂłn de advertencia
getIt<NotificationViewModel>().showWarning(
  message: 'Verifique los datos ingresados',
);

// NotificaciĂłn de error desde ErrorItem
getIt<NotificationViewModel>().showError(
  ErrorItem(
    message: 'No se pudo guardar el registro',
    title: 'Error de Base de Datos',
    errorLevel: ErrorLevelEnum.severe,
  ),
);

4. Uso con ViewModels y OperationResultMixin

class PatientViewModel extends ChangeNotifier with OperationResultMixin {
  final SavePatientUseCase _savePatientUseCase;

  PatientViewModel(this._savePatientUseCase);

  Future<void> savePatient(Patient patient) async {
    final result = await _savePatientUseCase(patient);
    
    result.fold(
      (error) => setOperationFailure(OperationFailure(error)),
      (_) => setOperationSuccess(
        OperationSuccess('Paciente guardado exitosamente'),
      ),
    );
  }
}

// En tu widget
class PatientFormPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<PatientViewModel>.value(
      value: getIt<PatientViewModel>(),
      child: Consumer<PatientViewModel>(
        builder: (context, viewModel, _) {
          // Escuchar resultados de operaciones
          final success = viewModel.operationSuccess;
          final failure = viewModel.operationFailure;

          if (success != null) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              getIt<NotificationViewModel>().showSuccess(
                message: success.message,
              );
              Navigator.pop(context);
            });
          } else if (failure != null) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              getIt<NotificationViewModel>().showError(
                failure.errorItem,
              );
            });
          }

          return Scaffold(
            appBar: AppBar(title: Text('Paciente')),
            body: PatientForm(viewModel: viewModel),
          );
        },
      ),
    );
  }
}

🎨 Personalización

ConfiguraciĂłn Global

void main() {
  // Configurar antes de iniciar la app
  NotificationConfig.global = NotificationConfig(
    successDuration: Duration(seconds: 3),
    errorDuration: null, // null = manual
    warningDuration: Duration(seconds: 4),
    infoDuration: Duration(seconds: 2),
    maxQueueSize: 5,
    animationDuration: Duration(milliseconds: 300),
    showCloseButton: true,
    enableAnimations: true,
    enableSwipeToDismiss: true,
  );

  runApp(MyApp());
}

Tema Personalizado

// Tema claro personalizado
NotificationTheme.current = NotificationTheme(
  successColor: Color(0xFF4CAF50),
  errorColor: Color(0xFFF44336),
  warningColor: Color(0xFFFF9800),
  infoColor: Color(0xFF2196F3),
  borderRadius: 12.0,
  elevation: 8.0,
  titleStyle: TextStyle(
    fontSize: 18,
    fontWeight: FontWeight.bold,
    color: Colors.white,
  ),
);

// O usar tema oscuro predefinido
NotificationTheme.current = NotificationTheme.dark;

Widget Personalizado

AppNotificationListener(
  customBuilder: (context, notification) {
    return Container(
      padding: EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.purple,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
        children: [
          Icon(Icons.star, color: Colors.white),
          SizedBox(width: 12),
          Expanded(
            child: Text(
              notification.message,
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  },
  child: YourApp(),
)

Notificaciones con Prioridad

// Prioridad baja
getIt<NotificationViewModel>().showInfo(
  message: 'SincronizaciĂłn en segundo plano',
  priority: NotificationPriority.low,
);

// Prioridad alta
getIt<NotificationViewModel>().showWarning(
  message: 'BaterĂ­a baja',
  priority: NotificationPriority.high,
);

// Prioridad crĂ­tica (interrumpe otras notificaciones)
getIt<NotificationViewModel>().showError(
  ErrorItem(
    message: 'ConexiĂłn perdida',
    errorLevel: ErrorLevelEnum.critical,
  ),
  priority: NotificationPriority.critical,
);

đź§Ş Testing

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_notification_system/flutter_notification_system.dart';

void main() {
  group('NotificationViewModel', () {
    late NotificationViewModel viewModel;

    setUp(() {
      viewModel = NotificationViewModel();
    });

    tearDown(() {
      viewModel.dispose();
    });

    test('should show notification', () {
      viewModel.showSuccess(message: 'Test');
      
      expect(viewModel.notification, isNotNull);
      expect(viewModel.notification?.message, equals('Test'));
    });
  });
}

🏛️ Arquitectura

lib/
├── src/
│   ├── models/              # Modelos de datos inmutables
│   │   ├── notification_data.dart
│   │   ├── notification_type.dart
│   │   ├── notification_priority.dart
│   │   ├── notification_config.dart
│   │   ├── operation_result.dart
│   │   └── error_item.dart
│   ├── view_models/         # Lógica de presentación
│   │   ├── notification_view_model.dart
│   │   └── operation_result_mixin.dart
│   ├── widgets/             # Componentes UI
│   │   ├── notification_listener.dart
│   │   ├── notification_presenter.dart
│   │   ├── custom_snackbar.dart
│   │   └── custom_dialog.dart
│   ├── utils/               # Utilidades
│   │   ├── notification_queue.dart
│   │   ├── notification_theme.dart
│   │   └── notification_animations.dart
│   └── di/                  # Inyección de dependencias
│       └── notification_di.dart
└── flutter_notification_system.dart

📚 Ejemplos Avanzados

Ver la carpeta /example para ejemplos completos de:

  • IntegraciĂłn con Clean Architecture
  • CRUD con notificaciones automáticas
  • PersonalizaciĂłn de temas
  • Testing completo
  • Manejo de errores

🤝 Mejoras sobre el Sistema Original

  1. Sistema de Prioridades: Cola inteligente que prioriza notificaciones crĂ­ticas
  2. Mejor ConfiguraciĂłn: ConfiguraciĂłn global y por notificaciĂłn
  3. Más Personalizable: Temas, animaciones y builders personalizados
  4. Testing Mejorado: Cobertura completa con tests unitarios
  5. Mejor Performance: Optimizaciones en la cola y listeners
  6. Más Tipos de Widgets: Múltiples estilos de SnackBars y Dialogs
  7. DocumentaciĂłn Completa: Ejemplos y guĂ­as detalladas

đź“„ Licencia

MIT License - ver LICENSE para detalles.


VersiĂłn: 1.0.0
Fecha: Enero 2026
Autor: Sistema mejorado para proyectos Flutter empresariales

Libraries

flutter_notification_system
Flutter Notification System Library