loris/lib/pages/notifications/notifications.dart

52 lines
1.4 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-08-15 21:30:53 +00:00
import 'dart:convert';
2022-07-02 22:03:10 +00:00
import 'package:flutter/material.dart';
2022-08-15 21:30:53 +00:00
import 'package:loris/pages/notifications/single_notif.dart';
2022-08-15 18:31:44 +00:00
import '../../business_logic/websocket.dart' as websocket;
2022-07-02 22:03:10 +00:00
final notifStream = StreamController<int>();
2022-08-15 18:31:44 +00:00
class Notifications extends StatefulWidget {
const Notifications({Key? key}) : super(key: key);
@override
State<Notifications> createState() => _NotificationsState();
}
class _NotificationsState extends State<Notifications> {
List<Widget> notifs = [];
2022-08-15 21:30:53 +00:00
2022-08-15 18:31:44 +00:00
@override
2022-08-15 21:30:53 +00:00
void initState() {
2022-08-15 18:31:44 +00:00
for (int i = 0; i < websocket.map.length; i++) {
final keyI = websocket.map.keys.toList()[i];
2022-08-15 21:30:53 +00:00
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"]),
);
2022-08-15 21:51:38 +00:00
if (mounted) {
setState(() {
notifStream.sink.add(1);
2022-08-15 21:51:38 +00:00
notifs.insert(0, notif);
});
}
2022-08-15 21:30:53 +00:00
}
});
2022-08-15 18:31:44 +00:00
}
2022-08-15 21:30:53 +00:00
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);
2022-08-15 18:31:44 +00:00
}
2022-07-02 22:03:10 +00:00
}