【发布时间】:2020-10-10 18:25:09
【问题描述】:
我为通知编写了代码,上面有一个徽章。在徽章上,我有总数的通知。我如何计算已读和未读通知?或在用户单击通知图标时删除徽章,然后在推送时显示徽章和新通知的数量。 这是我的代码:
// 这里我有一个通知图标和徽章,上面有通知总数
Center(
child: InkWell(
onTap: () {
if (currentUser.value.apiToken != null) {
Navigator.of(context).pushNamed(
'/NotificationsWidget',
);
} else {
Navigator.of(context).pushNamed('/Login');
}
},
child: Container(
child: Stack(
alignment: AlignmentDirectional.bottomEnd,
children: <Widget>[
Icon(
Icons.notifications_none,
color: this.widget.iconColor,
size: 30,
),
Positioned(
child: Container(
child: Text(
_con.notifications.length.toString(),
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption.merge(
TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 8),
),
),
padding: EdgeInsets.all(0),
decoration: BoxDecoration(
color: this.widget.labelColor,
borderRadius:
BorderRadius.all(Radius.circular(10))),
constraints: BoxConstraints(
minWidth: 13,
maxWidth: 13,
minHeight: 13,
maxHeight: 13),
),
),
],
),
),
),
);
// 这里有控制器,我正在监听通知
void listenForNotifications({String message}) async {
isPageLoading = true;
setState(() {
notifications.clear();
});
final Stream<model.Notification> stream = await getNotifications();
stream.listen((model.Notification _notification) {
setState(() {
notifications.add(_notification);
});
}, onError: (a) {
setState(() {
isPageLoading = false;
});
print(a);
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(S.current.verify_your_internet_connection),
));
}, onDone: () {
setState(() {
isPageLoading = false;
});
if (message != null) {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(message),
));
}
});
}
【问题讨论】:
标签: flutter push-notification notifications badge