【发布时间】:2021-03-17 10:05:42
【问题描述】:
我有点困惑,为什么当我选择多个商品时购物车计数器没有更新。它只显示 0。我必须在选项卡之间切换才能让计数器更改为正确的值。
购物车
class CartBloc{
List<ProductModel> cart = [];
double totalCartPrice = 0;
int cartCount = 0;
final _cartController = StreamController.broadcast();
Stream get getCartStream => _cartController.stream;
void addToCart(ProductModel product) {
cart.add(product);
cartCount = cartCount + 1;
totalCartPrice = totalCartPrice + double.parse(product.price);
_cartController.sink.add(cart);
}
void removeFromCart(ProductModel product) {
cart.remove(product);
cartCount = cartCount - 1;
totalCartPrice = totalCartPrice - double.parse(product.price);
_cartController.sink.add(cart);
}
void dispose() {
_cartController?.close();
}
}
final cartBloc = CartBloc();
主屏幕
BottomNavyBarItem(
textAlign: TextAlign.center,
activeColor: Color(0xFF010101),
title: Text(' CART',style: TextStyle(
color:Style.Colors.mainColor,fontSize: 13.0
)),
icon: Padding(
padding: EdgeInsets.only(left:5.0),
child: Badge(
badgeColor: Style.Colors.mainColor,
badgeContent: Text(cartBloc.cartCount.toString(),style: TextStyle(fontWeight: FontWeight.bold),),
child: Icon(
SimpleLineIcons.basket,
size:18.0,
color:_currentIndex == 1 ? Style.Colors.mainColor:Colors.white
),
)
)
),
【问题讨论】: