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: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<NotificationCenterDemo> {
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<NotificationCenterDemo> {
),
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<NotificationCenterDemo> {
],
),
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.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;
}

View file

@ -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',

View file

@ -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');