From 08d1784400d8120f7797c2c3e8db49b8a3a1fa8e Mon Sep 17 00:00:00 2001 From: Vick Top Date: Mon, 15 Apr 2024 09:56:49 +0200 Subject: [PATCH] feat: put in dialog and check bool check for popup type --- .../example/lib/main.dart | 56 +++++++++---------- .../lib/src/models/notification_config.dart | 4 ++ .../lib/src/notification_snackbar.dart | 5 +- .../firebase_notification_service.dart | 10 +++- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/packages/flutter_notification_center/example/lib/main.dart b/packages/flutter_notification_center/example/lib/main.dart index 2cad5de..d4c331f 100644 --- a/packages/flutter_notification_center/example/lib/main.dart +++ b/packages/flutter_notification_center/example/lib/main.dart @@ -1,5 +1,4 @@ import 'package:example/custom_notification.dart'; -import 'package:flutter_notification_center/src/notification_dialog.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -52,28 +51,36 @@ class NotificationCenterDemo extends StatefulWidget { } class _NotificationCenterDemoState extends State { + late NotificationConfig config; + @override - Widget build(BuildContext context) { + void initState() { + super.initState(); var service = FirebaseNotificationService( newNotificationCallback: (notification) { - ScaffoldMessenger.of(context).showSnackBar(NotificationSnackbarWidget( - title: notification.title, - body: notification.body, - datetimePublished: DateTime.now(), - )); - - // showDialog( - // context: context, - // builder: (context) => NotificationDialog( - // title: notification.title, - // body: notification.body, - // datetimePublished: notification.dateTimePushed), - // ); - debugPrint('New notification: ${notification.title}'); + if (config.showAsSnackBar) { + ScaffoldMessenger.of(context).showSnackBar( + NotificationSnackbar( + title: notification.title, + body: notification.body, + datetimePublished: DateTime.now(), + ), + ); + } else { + showDialog( + context: context, + builder: (context) => NotificationDialog( + title: notification.title, + body: notification.body, + datetimePublished: notification.dateTimePushed, + ), + ); + } }, ); - var config = NotificationConfig( + config = NotificationConfig( service: service, + showAsSnackBar: false, notificationWidgetBuilder: (notification, context) => CustomNotificationWidget( notification: notification, @@ -98,7 +105,10 @@ class _NotificationCenterDemoState extends State { ), seperateNotificationsWithDivider: true, ); + } + @override + Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Notification Center Demo'), @@ -110,18 +120,6 @@ class _NotificationCenterDemoState extends State { ], ), body: const SizedBox.shrink(), - floatingActionButton: FloatingActionButton( - onPressed: () { - service.pushNotification( - NotificationModel( - id: UniqueKey().toString(), - title: 'Test', - body: 'This is a test', - scheduledFor: DateTime.now(), - ), - ); - }, - ), ); } } diff --git a/packages/flutter_notification_center/lib/src/models/notification_config.dart b/packages/flutter_notification_center/lib/src/models/notification_config.dart index b6e8f94..b40fe30 100644 --- a/packages/flutter_notification_center/lib/src/models/notification_config.dart +++ b/packages/flutter_notification_center/lib/src/models/notification_config.dart @@ -15,6 +15,7 @@ class NotificationConfig { this.seperateNotificationsWithDivider = true, this.translations = const NotificationTranslations(), this.notificationWidgetBuilder, + this.showAsSnackBar = true, }); /// The notification service to use for delivering notifications. @@ -29,4 +30,7 @@ class NotificationConfig { /// Widget for building each notification item. final Widget Function(NotificationModel, BuildContext)? notificationWidgetBuilder; + + /// Whether to show notifications as snackbars. If false show notifications as a dialog. + final bool showAsSnackBar; } diff --git a/packages/flutter_notification_center/lib/src/notification_snackbar.dart b/packages/flutter_notification_center/lib/src/notification_snackbar.dart index 41b7062..264936d 100644 --- a/packages/flutter_notification_center/lib/src/notification_snackbar.dart +++ b/packages/flutter_notification_center/lib/src/notification_snackbar.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; -class NotificationSnackbarWidget extends SnackBar { - NotificationSnackbarWidget({ +class NotificationSnackbar extends SnackBar { + NotificationSnackbar({ super.key, required String title, required String body, @@ -39,7 +39,6 @@ class NotificationSnackbarWidget extends SnackBar { ), ], ), - backgroundColor: Colors.grey.shade700, duration: const Duration(seconds: 8), action: SnackBarAction( label: 'Dismiss', diff --git a/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart b/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart index 0a57eae..15ec201 100644 --- a/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart +++ b/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart @@ -45,8 +45,14 @@ class FirebaseNotificationService await notifications.doc(notification.id).set(notificationMap); listOfActiveNotifications.add(notification); - onNewNotification?.call(notification) ?? - newNotificationCallback(notification); + + //Show popup with notification contents + if (onNewNotification != null) { + onNewNotification(notification); + } else { + newNotificationCallback(notification); + } + notifyListeners(); } catch (e) { debugPrint('Error creating document: $e');