我读取了用户内容 (userCart
) 列表 Item 并将 shortInfo
读取为用户添加到购物车的产品的 ID,因此我可以看到项目的总价,但它不工作。
我认为它是可读的,如果有人能理解这个问题,请帮助我
child: Scaffold(
drawer: MyDrawer(),
//checkout button
floatingActionButton: FloatingActionButton.extended(
elevation: 10.0,
label: Text("Check Out"),
icon: Icon(Icons.navigate_next),
backgroundColor: Colors.pinkAccent,
onPressed: (){
if(EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList).length == 1)
{
Fluttertoast.showToast(msg: "Your Cart is empty!");
}
else
{
Route route = MaterialPageRoute(
builder: (_) => Address(totalAmount: totalAmount,));
Navigator.pushReplacement(context, route);
}
},
),
appBar:MyAppBar(),
body: CustomScrollView(
slivers: [
// total price of items
SliverToBoxAdapter(
child: Consumer2<TotalAmount, CartItemCounter>(
builder: (context, amountProvider, cartProvider, c){
return Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: cartProvider.count == 0
? Container()
: Text("Total Price: \$${amountProvider.totalAmount.toString()}",
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
fontWeight: FontWeight.w700,
),
),
),
);
}
),
),
//items in the cart page
StreamBuilder<QuerySnapshot>(
stream: EcommerceApp.firestore.collection("/items")
.where("shortInfo", whereIn:EcommerceApp.sharedPreferences.getStringList(EcommerceApp.userCartList)).snapshots(),
builder: (context, snapshots){
return !snapshots.hasData
? SliverToBoxAdapter(child: Center(child: circularProgress(),),)
: snapshots.data.documents.length == 0
? beginBuildinCart()
: SliverList(
delegate: SliverChildBuilderDelegate(
(context, index)
{
ItemModel model = ItemModel.fromJson(snapshots.data.documents[index].data);
if(index == 0){
totalAmount = 0;
totalAmount = model.price + totalAmount;
}
else
{
totalAmount = model.price + totalAmount;
}
if(snapshots.data.documents.length - 1 == index){
WidgetsBinding.instance.addPostFrameCallback((t) {
Provider.of<TotalAmount>(context, listen: false).display(totalAmount);
});
}
return sourceInfo(model, context, removeCartFunction: () => removeItemFromUserCart(model.shortInfo) );
},
childCount: snapshots.hasData ? snapshots.data.documents.length : 0
),
);
},
),
],
),
),
);
}
beginBuildinCart(){
}
removeItemFromUserCart(String shortInfoAsId){
}
}