loris/lib/pages/notifications/notifications.dart

52 lines
1.4 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:loris/pages/notifications/single_notif.dart';
import '../../business_logic/websocket.dart' as websocket;
final notifStream = StreamController<int>();
class Notifications extends StatefulWidget {
const Notifications({Key? key}) : super(key: key);
@override
State<Notifications> createState() => _NotificationsState();
}
class _NotificationsState extends State<Notifications> {
List<Widget> notifs = [];
@override
void initState() {
for (int i = 0; i < websocket.map.length; i++) {
final keyI = websocket.map.keys.toList()[i];
websocket.map[keyI]!["home"]!.stream.listen((event) async {
Map<String, dynamic> json = jsonDecode(event);
if (json["event"] == "notification") {
SingleNotif notif = SingleNotif(
content: jsonDecode(json["payload"]),
);
if (mounted) {
setState(() {
notifStream.sink.add(1);
notifs.insert(0, notif);
});
}
}
});
}
super.initState();
}
@override
Widget build(BuildContext context) {
return ListView.separated(
itemBuilder: ((context, index) => notifs[index]),
separatorBuilder: (context, index) => const SizedBox(
height: 8,
),
itemCount: notifs.length);
}
}