flutter 如何在导航栏动态更新徽章计数?

b4wnujal  于 2023-03-13  发布在  Flutter
关注(0)|答案(1)|浏览(128)

我的flutter应用程序中有以下导航栏:

BottomNavigationBar(
  items: [
    const BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    BottomNavigationBarItem(
      icon: appState.itemCard.cardItems.isNotEmpty   // only show Badge when items are cardItems
          ? badges.Badge(                            // This is the Badge
              badgeContent: Text(appState
                  .itemCard.cardItems.values
                  .reduce((a, b) => a + b)
                  .toString()),
              child: const Icon(Icons.shopping_cart),
            )
          : const Icon(Icons.shopping_cart),
      label: 'Card',
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.menu),
      label: 'Menu',
    ),
  ],
  currentIndex: selectedIndex,
  onTap: (value) {
    setState(() {
      selectedIndex = value;
    });
  },
)

问题是,当我对itemCard.cardItems进行更改时,徽章计数不会更新,直到我单击另一个NavigationBarItem(如Home)。
当我向itemCard.cardItems添加物品时,如何实现徽章立即更新?

编辑-解决方案,谢谢@Abhishek Karad

我已经为ItemCard类中的值创建了一个ValueNotifier

class ItemCard {
  Map<Item, int> cardItems = {};
  double totalPrice = 0.0;
  ValueNotifier<int> totalItems = ValueNotifier<int>(0); // <-- this one
}

在下一步中,我将相应的BottomNavigationBarItem的图标 Package 为ValueListenableBuilder

BottomNavigationBarItem(
  icon: ValueListenableBuilder( // wrapped Icon
      builder: (context, value, child) => badges.Badge(
            showBadge:
                appState.itemCard.totalItems.value > 0,
            badgeContent: Text(appState
                .itemCard.totalItems.value
                .toString()),
            child: const Icon(Icons.shopping_cart),
          ),
      valueListenable: appState.itemCard.totalItems), // ValueNotifier
  label: 'Card',
),
4ktjp1zp

4ktjp1zp1#

假设您有

List<CartModel> appState.itemCard.cardItems =[]

在AppState文件中
将其替换为

ValueNotifier<List<CartModel>> appState.itemCard.cardItems =ValueNotifier<List<CartModel>>([]);

并更换

BottomNavigationBarItem(
      icon: appState.itemCard.cardItems.isNotEmpty   // only show Badge when items are cardItems
          ? badges.Badge(                            // This is the Badge
              badgeContent: Text(appState
                  .itemCard.cardItems.values
                  .reduce((a, b) => a + b)
                  .toString()),
              child: const Icon(Icons.shopping_cart),
            )
          : const Icon(Icons.shopping_cart),
      label: 'Card',
    ),

ValueListenableBuilder(builder : (BuildContext context, int value, Widget? child) {
            // This builder will only get called when the _counter
            // is updated.
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Text('$value'),
                child!,
              ],
            );},valueListenable: appState
                  .itemCard.cardItems)

如果您已经在使用ValueNotifier,那么只需使用ValueListenableBuilder Package 您的小部件
了解更多https://api.flutter.dev/flutter/widgets/ValueListenableBuilder-class.html

相关问题