Flutter-收到通知时更改应用栏图标 [英] Flutter - change appbar icon when receiving notification
问题描述
我正在使用FirebaseMessaging在我的应用程序上推送通知.
I am using FirebaseMessaging to push notifications on my app.
因此我可以使用以下代码处理这些通知:
So I can handle these notification with this code :
firebaseMessaging.configure(
onLaunch: (Map<String, dynamic> msg) {
print("onLaunch called");
}, onResume: (Map<String, dynamic> msg) {
print("onResume called");
}, onMessage: (Map<String, dynamic> msg) {
print("onMessage called : " + msg.toString());
});
收到通知后,我想在应用栏中的图标上显示这个小"1"
When I receive a notification, I want to display this little '1' on my icon in my appbar
我的问题是:我不知道如何动态更改应用程序栏上所有页面的响铃图标(而且我无法在应用程序栏中调用setState)
My problem is : I don't know how to change my bell icon dynamically on my appbar for all pages (and I can't call setState in my appbar)
推荐答案
我认为解决您的问题非常简单,您只需要使用Stateful类和自定义图标即可,如下所示:
I think is pretty simple to solve your problem you only need to use a Stateful class and a custom icon as below snippet:
Widget myAppBarIcon(){
return Container(
width: 30,
height: 30,
child: Stack(
children: [
Icon(
Icons.notifications,
color: Colors.black,
size: 30,
),
Container(
width: 30,
height: 30,
alignment: Alignment.topRight,
margin: EdgeInsets.only(top: 5),
child: Container(
width: 15,
height: 15,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffc32c37),
border: Border.all(color: Colors.white, width: 1)),
child: Padding(
padding: const EdgeInsets.all(0.0),
child: Center(
child: Text(
_counter.toString(),
style: TextStyle(fontSize: 10),
),
),
),
),
),
],
),
);
}
,稍后您可以在应用程序栏上添加此图标(领导或操作).正如您看到的那样,当我启动新的Flutter项目时,以示例代码为基础的任何触摸都会改变Text值,它包含一种计算触摸浮动按钮并更改状态的次数的方法:
and later you can include this icon on your app bar(leading or action). As you can see the Text value change with any touch I used as base the example code when you start a new Flutter project it contains a method to count how many times you touch the floating button and change the state:
void _incrementCounter() {
setState(() {
_counter++;
});
}
希望对您有帮助
这篇关于Flutter-收到通知时更改应用栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!