2022-09-11 11:07:04 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2023-01-26 09:47:30 +01:00
|
|
|
import 'package:badges/badges.dart' as b;
|
2022-09-11 11:07:04 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
|
|
|
import 'matrix.dart';
|
|
|
|
|
|
|
|
class UnreadRoomsBadge extends StatelessWidget {
|
|
|
|
final bool Function(Room) filter;
|
2023-01-26 09:47:30 +01:00
|
|
|
final b.BadgePosition? badgePosition;
|
2022-09-11 11:07:04 +02:00
|
|
|
final Widget? child;
|
|
|
|
|
|
|
|
const UnreadRoomsBadge({
|
|
|
|
Key? key,
|
|
|
|
required this.filter,
|
|
|
|
this.badgePosition,
|
|
|
|
this.child,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return StreamBuilder(
|
2023-03-02 10:57:52 +01:00
|
|
|
stream: Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.onSync
|
|
|
|
.stream
|
|
|
|
.where((syncUpdate) => syncUpdate.hasRoomUpdate),
|
|
|
|
builder: (context, _) {
|
|
|
|
final unreadCount = Matrix.of(context)
|
2022-09-11 11:07:04 +02:00
|
|
|
.client
|
2023-03-02 10:57:52 +01:00
|
|
|
.rooms
|
|
|
|
.where(filter)
|
|
|
|
.where((r) => (r.isUnread || r.membership == Membership.invite))
|
|
|
|
.length;
|
|
|
|
return b.Badge(
|
2023-05-23 15:02:15 +02:00
|
|
|
alignment: Alignment.bottomRight,
|
2023-03-02 10:57:52 +01:00
|
|
|
badgeContent: Text(
|
|
|
|
unreadCount.toString(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onPrimary,
|
|
|
|
fontSize: 12,
|
2022-09-11 11:07:04 +02:00
|
|
|
),
|
2023-03-02 10:57:52 +01:00
|
|
|
),
|
|
|
|
showBadge: unreadCount != 0,
|
2023-05-23 15:02:15 +02:00
|
|
|
animationType: b.BadgeAnimationType.scale,
|
|
|
|
badgeColor: Theme.of(context).colorScheme.primary,
|
2023-03-02 10:57:52 +01:00
|
|
|
position: badgePosition,
|
2023-05-23 15:02:15 +02:00
|
|
|
elevation: 4,
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.background,
|
|
|
|
width: 2,
|
2023-03-02 10:57:52 +01:00
|
|
|
),
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2022-09-11 11:07:04 +02:00
|
|
|
}
|
|
|
|
}
|