-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications_service_widget.dart
49 lines (37 loc) · 1.34 KB
/
notifications_service_widget.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'dart:async';
import 'package:flutter/material.dart';
import 'model/services/notification_service.dart';
import 'service_locator.dart';
class NotificationsServiceWidget extends StatefulWidget {
final Widget child;
const NotificationsServiceWidget({super.key, required this.child});
@override
State<NotificationsServiceWidget> createState() => _NotificationsServiceWidgetState();
}
class _NotificationsServiceWidgetState extends State<NotificationsServiceWidget> {
final NotificationService _notificationService = sl();
StreamSubscription<String>? _snackSubscription;
SnackBar _buildSnackbar(BuildContext context, String message) {
return SnackBar(
content: Text(message, style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Theme.of(context).colorScheme.background)),
backgroundColor: Theme.of(context).colorScheme.onBackground,
);
}
@override
void initState() {
super.initState();
if (_snackSubscription != null) _snackSubscription?.cancel();
_snackSubscription = _notificationService.subscribeToSnackStream((value) {
ScaffoldMessenger.of(context).showSnackBar( _buildSnackbar(context, value) );
});
}
@override
Widget build(BuildContext context) {
return widget.child;
}
@override
void dispose() {
super.dispose();
_snackSubscription?.cancel();
}
}