show indicator when theres new notifications

This commit is contained in:
zoe 2022-08-16 16:13:29 +02:00
parent 10d1771af9
commit 6efa92c289
3 changed files with 26 additions and 4 deletions

View File

@ -1,9 +1,12 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:loris/pages/notifications/single_notif.dart'; import 'package:loris/pages/notifications/single_notif.dart';
import '../../business_logic/websocket.dart' as websocket; import '../../business_logic/websocket.dart' as websocket;
final notifStream = StreamController<int>();
class Notifications extends StatefulWidget { class Notifications extends StatefulWidget {
const Notifications({Key? key}) : super(key: key); const Notifications({Key? key}) : super(key: key);
@ -26,6 +29,7 @@ class _NotificationsState extends State<Notifications> {
); );
if (mounted) { if (mounted) {
setState(() { setState(() {
notifStream.sink.add(1);
notifs.insert(0, notif); notifs.insert(0, notif);
}); });
} }

View File

@ -9,7 +9,6 @@ class SingleNotif extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
print(content);
return Text(content["id"].toString()); return Text(content["id"].toString());
} }
} }

View File

@ -16,6 +16,19 @@ class MainScaffold extends StatefulWidget {
class _MainScaffoldState extends State<MainScaffold> { class _MainScaffoldState extends State<MainScaffold> {
int index = 0; int index = 0;
int unreadNotifs = 0;
@override
void initState() {
notifStream.stream.listen((event) {
if (index != 2) {
setState(() {
unreadNotifs = event;
});
}
});
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -54,8 +67,12 @@ class _MainScaffoldState extends State<MainScaffold> {
floatingActionButton: buttons[index], floatingActionButton: buttons[index],
bottomNavigationBar: BottomAppBar( bottomNavigationBar: BottomAppBar(
child: NavigationBar( child: NavigationBar(
onDestinationSelected: (index) => onDestinationSelected: (index) => setState(() {
setState(() => this.index = index), this.index = index;
if (index == 2) {
unreadNotifs = 0;
}
}),
selectedIndex: index, selectedIndex: index,
destinations: [ destinations: [
NavigationDestination( NavigationDestination(
@ -63,7 +80,9 @@ class _MainScaffoldState extends State<MainScaffold> {
NavigationDestination( NavigationDestination(
icon: const Icon(Icons.chat), label: "chat".i18n()), icon: const Icon(Icons.chat), label: "chat".i18n()),
NavigationDestination( NavigationDestination(
icon: const Icon(Icons.notifications), icon: Icon((unreadNotifs >= 1)
? Icons.notifications_active
: Icons.notifications),
label: "notifications".i18n()), label: "notifications".i18n()),
NavigationDestination( NavigationDestination(
icon: const Icon(Icons.settings), label: "settings".i18n()), icon: const Icon(Icons.settings), label: "settings".i18n()),