Flutter徽章包 - 如何不动态显示零/更新?

发布于 01-22 13:41 字数 3303 浏览 3 评论 0原文

我正在使用徽章包装flutter以显示否。在底部导航栏中,看不见的通知是徽章。 我的底部Navbar小部件

  Widget homeBottomBar() {
    final notificationNotifier =
        Provider.of<NotificationNotifier>(context, listen: false);
    ProfileModel profile = profileNotifier(true).profile;
    var _unseenCount;

    return Container(
      height: MediaQuery.of(context).size.height * 0.072,
      decoration: const BoxDecoration(
        border: Border(
          top: BorderSide(
            color: Colors.black38,
            width: 0.54,
          ),
        ),
      ),
      child: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey[700],
        selectedItemColor: Colors.black,
        showSelectedLabels: true,
        showUnselectedLabels: false,
        currentIndex: bottomBarIndex,
        selectedLabelStyle: const TextStyle(fontWeight: FontWeight.bold),
        onTap: (index) => setState(() => bottomBarIndex = index),
        items: [
          const BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.house_fill), label: 'Feeds'),
          const BottomNavigationBarItem(
              icon: Icon(Icons.people_alt_rounded), label: 'People'),
          BottomNavigationBarItem(
              icon: profile.profileId.isNotEmpty
                  ? Badge(
                      child: Icon(
                        CupertinoIcons.bell_solid,
                        color: bottomBarIndex == 3 ? Colors.black : Colors.grey,
                      ),
                      badgeColor: Colors.red,
                      showBadge: _unseenCount == 0 ? false : true,
                      animationType: BadgeAnimationType.fade,
                      badgeContent: FutureBuilder(
                        future: notificationNotifier.getUnseenCount(
                            profileId: profile.profileId),
                        builder: (context, snapshot) {
                          _unseenCount = snapshot.data;
                          print(_unseenCount.runtimeType);
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return const SizedBox(
                              height: 22.5,
                              width: 0,
                            );
                          }
                          return Column(
                            children: [
                              Text(
                                _unseenCount.toString(),
                                style: const TextStyle(
                                  fontSize: 12.6,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    )
                  : const Icon(CupertinoIcons.bell_solid),
              label: 'Notifications'),
        ],
      ),
    );
  }

尝试使用_unseencount设置Showbadge Boolean,但这也无济于事。

这就是现在的样子:

如何正确设置布尔式Showbadge,以使其动态并在计数发生更改时得到更新?

I am using Badges package with flutter to show no. of unseen notifications as badge for an icon in the bottom navigation bar.
My bottom navbar widget

  Widget homeBottomBar() {
    final notificationNotifier =
        Provider.of<NotificationNotifier>(context, listen: false);
    ProfileModel profile = profileNotifier(true).profile;
    var _unseenCount;

    return Container(
      height: MediaQuery.of(context).size.height * 0.072,
      decoration: const BoxDecoration(
        border: Border(
          top: BorderSide(
            color: Colors.black38,
            width: 0.54,
          ),
        ),
      ),
      child: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey[700],
        selectedItemColor: Colors.black,
        showSelectedLabels: true,
        showUnselectedLabels: false,
        currentIndex: bottomBarIndex,
        selectedLabelStyle: const TextStyle(fontWeight: FontWeight.bold),
        onTap: (index) => setState(() => bottomBarIndex = index),
        items: [
          const BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.house_fill), label: 'Feeds'),
          const BottomNavigationBarItem(
              icon: Icon(Icons.people_alt_rounded), label: 'People'),
          BottomNavigationBarItem(
              icon: profile.profileId.isNotEmpty
                  ? Badge(
                      child: Icon(
                        CupertinoIcons.bell_solid,
                        color: bottomBarIndex == 3 ? Colors.black : Colors.grey,
                      ),
                      badgeColor: Colors.red,
                      showBadge: _unseenCount == 0 ? false : true,
                      animationType: BadgeAnimationType.fade,
                      badgeContent: FutureBuilder(
                        future: notificationNotifier.getUnseenCount(
                            profileId: profile.profileId),
                        builder: (context, snapshot) {
                          _unseenCount = snapshot.data;
                          print(_unseenCount.runtimeType);
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return const SizedBox(
                              height: 22.5,
                              width: 0,
                            );
                          }
                          return Column(
                            children: [
                              Text(
                                _unseenCount.toString(),
                                style: const TextStyle(
                                  fontSize: 12.6,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    )
                  : const Icon(CupertinoIcons.bell_solid),
              label: 'Notifications'),
        ],
      ),
    );
  }

I tried setting showBadge boolean with _unseenCount but that didn't help either.

This is how it looks now:
This is how it looks now

How to set the boolean showBadge correctly so that it's dynamic and gets updated if the count changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零2025-01-29 13:41:12

您正在使用FutureBuilder badgecontent,该可以正确抓取该值并构建徽章内容。但是,Showbadge没有将其放置在此构建器下,我认为这会导致您描述的行为。

一种可能的解决方案是将FutureBuilder提升到一个级别,以重建整个徽章 widget一旦未来完成

You're using FutureBuilder for the badgeContent which grab the value correctly and build the badge content. However, showBadge is not placed under this builder which I believe is causing the behavior you're describing.

One possible solution will be moving the FutureBuilder up one level to rebuild the entire Badge widget once the future is done

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文