android - Flutter - 收到通知时更改应用栏图标

标签 android dart flutter notifications

我正在使用 FirebaseMessaging 在我的应用程序上推送通知。

所以我可以用这段代码处理这些通知:

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”

icon

我的问题是:我不知道如何为所有页面在我的应用栏上动态更改我的铃铛图标(而且我无法在我的应用栏中调用 setState)

最佳答案

我认为解决您的问题非常简单,您只需要使用一个有状态类和一个自定义图标,如下所示:

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),
              ),
            ),
          ),
        ),
      ),
    ],
  ),
);
}

以后您可以将此图标包含在您的应用程序栏中(引导或操作)。正如您所看到的,Text 值随任何触摸而变化,我在开始一个新的 Flutter 项目时用作示例代码的基础,它包含一个方法来计算您触摸 float 按钮并更改状态的次数:

void _incrementCounter() {
setState(() {
  _counter++;
});
}

希望对你有帮助

this is my example

关于android - Flutter - 收到通知时更改应用栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730390/

相关文章:

android - 如何在 Kotlin 中实现 MediaPlayer onErrorListener

android - startService 的隐式 Intent 不安全 InAppPurchase

android - 将静态 fragment 与新设计支持库的 CoordinatorLayout 一起使用

java - 地理位置权限不足

flutter - 为什么在 Flutter 测试期间使用 rootBundle.load 会得到 "Null check operator used on a null value"?

Flutter:如何在我的应用程序中显示印地文字符?

android - 在 flutter 应用程序中添加时间选择器的正确方法是什么?

flutter - 如何在 flutter 中将 child 定位在堆栈中?

xcode - 如何解决Flutter iOS Build缺少的必需文件

flutter - 从 ElevatedButton.styleFrom() 更改 ElevatedButton 禁用状态的文本颜色