feat: add config option to fully disable popups

This commit is contained in:
Vick Top 2024-04-15 10:23:41 +02:00
parent 9ea998119f
commit b0be65999c
6 changed files with 57 additions and 30 deletions

View file

@ -23,6 +23,7 @@ The `NotificationConfig` has its own parameters, as specified below:
| seperateNotificationsWithDivider | If true notifications will be seperated with dividers within the notification center | | seperateNotificationsWithDivider | If true notifications will be seperated with dividers within the notification center |
| translations | The translations that will be used | | translations | The translations that will be used |
| notificationWidgetBuilder | The widget that defines the styles and logic for every notification | | notificationWidgetBuilder | The widget that defines the styles and logic for every notification |
| enableNotificationPopups | If set to false no popups will be shown if a new notification is pushed |
| showAsSnackBar | If true notifications popups will show as snackbar. If false shown as dialog| | showAsSnackBar | If true notifications popups will show as snackbar. If false shown as dialog|
The `notificationWidgetBuilder` expects the following parameters, as specified below: The `notificationWidgetBuilder` expects the following parameters, as specified below:

View file

@ -52,34 +52,18 @@ class NotificationCenterDemo extends StatefulWidget {
class _NotificationCenterDemoState extends State<NotificationCenterDemo> { class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
late NotificationConfig config; late NotificationConfig config;
late PopupHandler popupHandler;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
var service = FirebaseNotificationService( var service =
newNotificationCallback: (notification) { FirebaseNotificationService(newNotificationCallback: (notification) {
if (config.showAsSnackBar) { popupHandler.handleNotificationPopup(notification);
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,
),
);
}
},
);
config = NotificationConfig( config = NotificationConfig(
service: service, service: service,
enableNotificationPopups: true,
showAsSnackBar: false, showAsSnackBar: false,
notificationWidgetBuilder: (notification, context) => notificationWidgetBuilder: (notification, context) =>
CustomNotificationWidget( CustomNotificationWidget(
@ -105,6 +89,7 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
), ),
seperateNotificationsWithDivider: true, seperateNotificationsWithDivider: true,
); );
popupHandler = PopupHandler(context: context, config: config);
} }
@override @override

View file

@ -8,6 +8,7 @@ export "src/models/notification_theme.dart";
export "src/models/notification_translation.dart"; export "src/models/notification_translation.dart";
export "src/notification_bell.dart"; export "src/notification_bell.dart";
export "src/notification_dialog.dart"; export "src/notification_dialog.dart";
export "src/popup_handler.dart";
export "src/notification_snackbar.dart"; export "src/notification_snackbar.dart";
export "src/notification_detail.dart"; export "src/notification_detail.dart";
export "src/notification_bell_story.dart"; export "src/notification_bell_story.dart";

View file

@ -10,13 +10,13 @@ class NotificationConfig {
/// to use. The [style] parameter is optional and defines the style of the /// to use. The [style] parameter is optional and defines the style of the
/// notification. The [translations] parameter is also optional and provides /// notification. The [translations] parameter is also optional and provides
/// translations for notification messages. /// translations for notification messages.
const NotificationConfig({ const NotificationConfig(
required this.service, {required this.service,
this.seperateNotificationsWithDivider = true, this.seperateNotificationsWithDivider = true,
this.translations = const NotificationTranslations(), this.translations = const NotificationTranslations(),
this.notificationWidgetBuilder, this.notificationWidgetBuilder,
this.showAsSnackBar = true, this.showAsSnackBar = true,
}); this.enableNotificationPopups = true});
/// The notification service to use for delivering notifications. /// The notification service to use for delivering notifications.
final NotificationService service; final NotificationService service;
@ -33,4 +33,7 @@ class NotificationConfig {
/// Whether to show notifications as snackbars. If false show notifications as a dialog. /// Whether to show notifications as snackbars. If false show notifications as a dialog.
final bool showAsSnackBar; final bool showAsSnackBar;
/// Whether to show notification popups.
final bool enableNotificationPopups;
} }

View file

@ -0,0 +1,37 @@
// Define a PopupHandler class to handle notification popups
import 'package:flutter/material.dart';
import 'package:flutter_notification_center/flutter_notification_center.dart';
class PopupHandler {
final BuildContext context;
final NotificationConfig config;
PopupHandler({
required this.context,
required this.config,
});
void handleNotificationPopup(NotificationModel notification) {
if (!config.enableNotificationPopups) return;
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,
),
);
}
}
}

View file

@ -46,7 +46,7 @@ class FirebaseNotificationService
listOfActiveNotifications.add(notification); listOfActiveNotifications.add(notification);
//Show popup with notification contents //Show popup with notification conte
if (onNewNotification != null) { if (onNewNotification != null) {
onNewNotification(notification); onNewNotification(notification);
} else { } else {