diff --git a/CHANGELOG.md b/CHANGELOG.md index c23c3c3..6303723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ +## [1.2.0] - 17 April 2024 + +* Add functionality to pin notifications by swiping + ## [1.1.0] - 15 April 2024 -* Initial Release +* Add option for snackbar and dialog popups ## [1.0.0] - 14 April 2024 diff --git a/packages/flutter_notification_center/example/lib/custom_notification.dart b/packages/flutter_notification_center/example/lib/custom_notification.dart index 2a6c186..9561e79 100644 --- a/packages/flutter_notification_center/example/lib/custom_notification.dart +++ b/packages/flutter_notification_center/example/lib/custom_notification.dart @@ -56,23 +56,44 @@ class CustomNotificationWidget extends StatelessWidget { : Dismissible( key: Key(notification.id), onDismissed: (direction) async { - await dismissNotification(notificationService, notification); + if (direction == DismissDirection.endToStart) { + await dismissNotification(notificationService, notification); + } else if (direction == DismissDirection.startToEnd) { + await pinNotification( + notificationService, notification, context); + } }, background: Container( - color: Colors.red, + color: const Color.fromRGBO(59, 213, 111, 1), + alignment: Alignment.centerLeft, + child: const Padding( + padding: EdgeInsets.only(left: 16.0), + child: Icon( + Icons.push_pin, + color: Colors.white, + ), + ), + ), + secondaryBackground: Container( + color: const Color.fromRGBO(255, 131, 131, 1), alignment: Alignment.centerRight, - child: const Icon( - Icons.delete, - color: Colors.white, + child: const Padding( + padding: EdgeInsets.only(right: 16.0), + child: Icon( + Icons.delete, + color: Colors.white, + ), ), ), child: GestureDetector( - onTap: () async => - _navigateToNotificationDetail(context, notification), + onTap: () async => _navigateToNotificationDetail( + context, + notification, + ), child: ListTile( leading: Icon( notification.icon, - color: style.leadingIconColor, + color: Colors.grey, ), title: Row( crossAxisAlignment: CrossAxisAlignment.start, @@ -80,7 +101,11 @@ class CustomNotificationWidget extends StatelessWidget { Expanded( child: Text( notification.title, - style: style.titleTextStyle, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w400, + fontSize: 16, + ), ), ), ], diff --git a/packages/flutter_notification_center/lib/src/models/notification.dart b/packages/flutter_notification_center/lib/src/models/notification.dart index 2461781..edcfbf1 100644 --- a/packages/flutter_notification_center/lib/src/models/notification.dart +++ b/packages/flutter_notification_center/lib/src/models/notification.dart @@ -85,7 +85,7 @@ class NotificationModel { final OcurringInterval? occuringInterval; /// Indicates if the notification is pinned. - final bool isPinned; + bool isPinned; /// Indicates if the notification has been read. bool isRead; diff --git a/packages/flutter_notification_center/lib/src/notification_center.dart b/packages/flutter_notification_center/lib/src/notification_center.dart index e1154d7..91f4223 100644 --- a/packages/flutter_notification_center/lib/src/notification_center.dart +++ b/packages/flutter_notification_center/lib/src/notification_center.dart @@ -136,17 +136,42 @@ class NotificationCenterState extends State { : Dismissible( key: Key(notification.id), onDismissed: (direction) async { - await dismissNotification( - widget.config.service, - notification, - context); + if (direction == + DismissDirection.endToStart) { + await dismissNotification( + widget.config.service, + notification, + context); + } else if (direction == + DismissDirection.startToEnd) { + await pinNotification( + widget.config.service, + notification, + context); + } }, background: Container( - color: Colors.red, + color: + const Color.fromRGBO(59, 213, 111, 1), + alignment: Alignment.centerLeft, + child: const Padding( + padding: EdgeInsets.only(left: 16.0), + child: Icon( + Icons.push_pin, + color: Colors.white, + ), + ), + ), + secondaryBackground: Container( + color: + const Color.fromRGBO(255, 131, 131, 1), alignment: Alignment.centerRight, - child: const Icon( - Icons.delete, - color: Colors.white, + child: const Padding( + padding: EdgeInsets.only(right: 16.0), + child: Icon( + Icons.delete, + color: Colors.white, + ), ), ), child: GestureDetector( @@ -238,6 +263,21 @@ Future dismissNotification( } } +Future pinNotification( + NotificationService notificationService, + NotificationModel notification, + BuildContext context, +) async { + await notificationService.pinActiveNotification(notification); + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text("Notification pinned"), + ), + ); + } +} + Future markNotificationAsRead( NotificationService notificationService, NotificationModel notification, diff --git a/packages/flutter_notification_center/lib/src/services/notification_service.dart b/packages/flutter_notification_center/lib/src/services/notification_service.dart index cd92147..22b7f15 100644 --- a/packages/flutter_notification_center/lib/src/services/notification_service.dart +++ b/packages/flutter_notification_center/lib/src/services/notification_service.dart @@ -44,6 +44,9 @@ abstract class NotificationService with ChangeNotifier { /// Dismisses an active notification. Future dismissActiveNotification(NotificationModel notification); + /// Pin an active notification. + Future pinActiveNotification(NotificationModel notification); + /// Marks a notification as read. Future markNotificationAsRead(NotificationModel notification); 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 b2cc09a..6b6711c 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 @@ -164,6 +164,21 @@ class FirebaseNotificationService } } + @override + Future pinActiveNotification( + NotificationModel notificationModel) async { + try { + DocumentReference documentReference = FirebaseFirestore.instance + .collection(FirebaseCollectionNames.activeNotifications) + .doc(notificationModel.id); + await documentReference.update({'isPinned': true}); + notificationModel.isPinned = true; + notifyListeners(); + } catch (e) { + debugPrint('Error updating document: $e'); + } + } + @override Future markNotificationAsRead( NotificationModel notificationModel) async { diff --git a/pubspec.yaml b/pubspec.yaml index 3f2a9d6..19044ba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ description: "A new Flutter project." # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 1.1.0 +version: 1.2.0 environment: sdk: '>=3.3.2 <4.0.0'