Flutter徽章包 - 如何不动态显示零/更新?
我正在使用徽章包装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.
How to set the boolean showBadge correctly so that it's dynamic and gets updated if the count changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您正在使用
FutureBuilder
badgecontent
,该可以正确抓取该值并构建徽章内容。但是,Showbadge
没有将其放置在此构建器下,我认为这会导致您描述的行为。一种可能的解决方案是将FutureBuilder提升到一个级别,以重建整个
徽章
widget一旦未来完成You're using
FutureBuilder
for thebadgeContent
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