Skip to content

Commit

Permalink
change to resolve reviews ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
bebaoboy committed Apr 22, 2024
1 parent 9cbea0b commit 2262eae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
7 changes: 5 additions & 2 deletions lib/src/navbar_decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) =>
Expand Down
18 changes: 10 additions & 8 deletions lib/src/navbar_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,27 @@ 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();
}

/// Use to set the visibility of a badge using its [index].
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<GlobalKey<NavigatorState>> value,
{List<NavbarBadge>? badgeList}) {
static void setKeys(List<GlobalKey<NavigatorState>> value) {
_keys = value;
}

/// The only place that init the badges.
static void setBadges(List<NavbarBadge>? badgeList) {
if (badgeList != null) {
_badges = badgeList;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/navbar_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ class _NavbarRouterState extends State<NavbarRouter>
final navbaritem = widget.destinations[i].navbarItem;
keys.add(GlobalKey<NavigatorState>());
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;
Expand Down

0 comments on commit 2262eae

Please sign in to comment.