diff --git a/lib/src/navbar_decoration.dart b/lib/src/navbar_decoration.dart index 1b7a9d8..2c05972 100644 --- a/lib/src/navbar_decoration.dart +++ b/lib/src/navbar_decoration.dart @@ -3,7 +3,10 @@ import 'package:navbar_router/navbar_router.dart'; class NavbarItem { const NavbarItem(this.iconData, this.text, - {this.backgroundColor, this.child, this.selectedIcon, this.badge}); + {this.backgroundColor, + this.child, + this.selectedIcon, + this.badge = const NavbarBadge()}); /// IconData for the navbar item final IconData iconData; @@ -23,7 +26,7 @@ class NavbarItem { final Widget? selectedIcon; /// Your initial badge configuration for this item, this is totally optional - final NavbarBadge? badge; + final NavbarBadge badge; @override bool operator ==(Object other) => diff --git a/lib/src/navbar_notifier.dart b/lib/src/navbar_notifier.dart index fe6e666..f7b7a71 100644 --- a/lib/src/navbar_notifier.dart +++ b/lib/src/navbar_notifier.dart @@ -44,8 +44,10 @@ class NavbarNotifier extends ChangeNotifier { static void updateBadge(int index, NavbarBadge badge) { if (index < 0 || index >= length) return; _badges[index] = badge; - // TODO: wonder if this will cause performance issue - NavbarNotifier.index = index; + + // We don't navigate to that item when we update its badge. So cannot use this. + // NavbarNotifier.index = index; + _singleton.notify(); } @@ -53,16 +55,16 @@ class NavbarNotifier extends ChangeNotifier { static void makeBadgeVisible(int index, bool visible) { if (index < 0 || index >= length) return; _badges[index] = _badges[index].copyWith(showBadge: visible); - _notifyIndexChangeListeners(index); + _singleton.notify(); } - /// Conveniently setup the badges if user choose to show them. Also the only place that init the badges. - /// - /// Will throw AssertionError if length of keys and given badgeList are not the same - static void setKeys(List> value, - {List? badgeList}) { + static void setKeys(List> value) { _keys = value; + } + + /// The only place that init the badges. + static void setBadges(List? badgeList) { if (badgeList != null) { _badges = badgeList; } diff --git a/lib/src/navbar_router.dart b/lib/src/navbar_router.dart index a297a7e..55eb836 100644 --- a/lib/src/navbar_router.dart +++ b/lib/src/navbar_router.dart @@ -204,11 +204,14 @@ class _NavbarRouterState extends State final navbaritem = widget.destinations[i].navbarItem; keys.add(GlobalKey()); items.add(navbaritem); - badges.add(navbaritem.badge ?? const NavbarBadge()); + badges.add(navbaritem.badge); } + NavbarNotifier.setKeys(keys); + // set badge list here - NavbarNotifier.setKeys(keys, badgeList: badges); + NavbarNotifier.setBadges(badges); NavbarNotifier.hideBadgeOnPageChanged = widget.hideBadgeOnPageChanged; + if (!isUpdate) { initAnimation(); NavbarNotifier.index = widget.initialIndex;