feat: put in dialog and check bool check for popup type

This commit is contained in:
Vick Top 2024-04-15 09:56:49 +02:00
parent 49c2418467
commit 08d1784400
4 changed files with 41 additions and 34 deletions

View file

@ -1,5 +1,4 @@
import 'package:example/custom_notification.dart'; import 'package:example/custom_notification.dart';
import 'package:flutter_notification_center/src/notification_dialog.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -52,28 +51,36 @@ class NotificationCenterDemo extends StatefulWidget {
} }
class _NotificationCenterDemoState extends State<NotificationCenterDemo> { class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
late NotificationConfig config;
@override @override
Widget build(BuildContext context) { void initState() {
super.initState();
var service = FirebaseNotificationService( var service = FirebaseNotificationService(
newNotificationCallback: (notification) { newNotificationCallback: (notification) {
ScaffoldMessenger.of(context).showSnackBar(NotificationSnackbarWidget( if (config.showAsSnackBar) {
title: notification.title, ScaffoldMessenger.of(context).showSnackBar(
body: notification.body, NotificationSnackbar(
datetimePublished: DateTime.now(), title: notification.title,
)); body: notification.body,
datetimePublished: DateTime.now(),
// showDialog( ),
// context: context, );
// builder: (context) => NotificationDialog( } else {
// title: notification.title, showDialog(
// body: notification.body, context: context,
// datetimePublished: notification.dateTimePushed), builder: (context) => NotificationDialog(
// ); title: notification.title,
debugPrint('New notification: ${notification.title}'); body: notification.body,
datetimePublished: notification.dateTimePushed,
),
);
}
}, },
); );
var config = NotificationConfig( config = NotificationConfig(
service: service, service: service,
showAsSnackBar: false,
notificationWidgetBuilder: (notification, context) => notificationWidgetBuilder: (notification, context) =>
CustomNotificationWidget( CustomNotificationWidget(
notification: notification, notification: notification,
@ -98,7 +105,10 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
), ),
seperateNotificationsWithDivider: true, seperateNotificationsWithDivider: true,
); );
}
@override
Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Notification Center Demo'), title: const Text('Notification Center Demo'),
@ -110,18 +120,6 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
], ],
), ),
body: const SizedBox.shrink(), body: const SizedBox.shrink(),
floatingActionButton: FloatingActionButton(
onPressed: () {
service.pushNotification(
NotificationModel(
id: UniqueKey().toString(),
title: 'Test',
body: 'This is a test',
scheduledFor: DateTime.now(),
),
);
},
),
); );
} }
} }

View file

@ -15,6 +15,7 @@ class NotificationConfig {
this.seperateNotificationsWithDivider = true, this.seperateNotificationsWithDivider = true,
this.translations = const NotificationTranslations(), this.translations = const NotificationTranslations(),
this.notificationWidgetBuilder, this.notificationWidgetBuilder,
this.showAsSnackBar = true,
}); });
/// The notification service to use for delivering notifications. /// The notification service to use for delivering notifications.
@ -29,4 +30,7 @@ class NotificationConfig {
/// Widget for building each notification item. /// Widget for building each notification item.
final Widget Function(NotificationModel, BuildContext)? final Widget Function(NotificationModel, BuildContext)?
notificationWidgetBuilder; notificationWidgetBuilder;
/// Whether to show notifications as snackbars. If false show notifications as a dialog.
final bool showAsSnackBar;
} }

View file

@ -1,8 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
class NotificationSnackbarWidget extends SnackBar { class NotificationSnackbar extends SnackBar {
NotificationSnackbarWidget({ NotificationSnackbar({
super.key, super.key,
required String title, required String title,
required String body, required String body,
@ -39,7 +39,6 @@ class NotificationSnackbarWidget extends SnackBar {
), ),
], ],
), ),
backgroundColor: Colors.grey.shade700,
duration: const Duration(seconds: 8), duration: const Duration(seconds: 8),
action: SnackBarAction( action: SnackBarAction(
label: 'Dismiss', label: 'Dismiss',

View file

@ -45,8 +45,14 @@ class FirebaseNotificationService
await notifications.doc(notification.id).set(notificationMap); await notifications.doc(notification.id).set(notificationMap);
listOfActiveNotifications.add(notification); listOfActiveNotifications.add(notification);
onNewNotification?.call(notification) ??
newNotificationCallback(notification); //Show popup with notification contents
if (onNewNotification != null) {
onNewNotification(notification);
} else {
newNotificationCallback(notification);
}
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
debugPrint('Error creating document: $e'); debugPrint('Error creating document: $e');