gpt4 book ai didi

dart - Flutter 更新购物车项目列表同时删除重复条目

转载 作者:行者123 更新时间:2023-12-03 03:17:54 26 4
gpt4 key购买 nike

我正在使用购物车应用程序并使用 flutter_redux 更新我的购物车。一切正常,但我的购物车列表中有重复的商品,如何删除这些重复的条目并添加数量。我想获得包含元素和数量的 list 。请帮助我处理这段代码,我可以在哪里添加逻辑并使用删除的重复项过滤列表。

这是我的代码:

cartItemReducer.dart

List<CartItem> cartItemReducer(List<CartItem> items, dynamic action)
{
if(action is AddItemAction)
{
return addItem(items, action);
}
else if (action is DeleteItemAction)
{
return deleteItem(items, action);
}

return items;
}

List<CartItem> addItem(List<CartItem> items, AddItemAction action)
{
return new List.from(items)..add(action.item);
}

List<CartItem> deleteItem(List<CartItem> items, DeleteItemAction action)
{

return new List.from(items)..remove(action.item);
}

通过此代码将商品添加到我的购物车:

Widget futureWidget()
{

return new FutureBuilder<List<ModelProductDetail>>(

future: getDetailProductFuture(),
builder: (BuildContext context, AsyncSnapshot snapshot){

if(snapshot.hasData)
{
Widget addRemoveButtonRedux = new StoreConnector<
List<CartItem>, OnItemAddedCallback>(
converter: (store)
=>(itemName) => store.dispatch(
AddItemAction(CartItem(itemName.product, itemName.quantity

))),
builder: (context, callback) =>
new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.minus_circled,
color: Colors.cyan,
semanticLabel: 'Subtract',
),
onPressed: quantity == 0 ? null : () => decrement(),
),

new Container(
decoration: new BoxDecoration(
border: new Border.all(
color: Colors.grey[700],
width: 0.5
)
),
child: new SizedBox(
width: 40.0,
height: 30.0,
child: new Center(
child: new Text('$quantity',
style: Theme
.of(context)
.textTheme
.subhead,
textAlign: TextAlign.center,),

),
),
),
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.plus_circled,
color: Colors.cyan,
semanticLabel: 'ADD',
),
onPressed: () => increment(
callback

),
),


],
),
) ,
);

//getDetailProduct();
Widget addRemoveButton = new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.minus_circled,
color: Colors.cyan,
semanticLabel: 'Subtract',
),
onPressed: quantity == 0 ? null : () => decrement(),
),

new Container(
decoration: new BoxDecoration(
border: new Border.all(
color: Colors.grey[700],
width: 0.5
)
),
child: new SizedBox(
width: 40.0,
height: 30.0,
child: new Center(
child: new Text('$quantity',
style: Theme
.of(context)
.textTheme
.subhead,
textAlign: TextAlign.center,),

),
),
),
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.plus_circled,
color: Colors.cyan,
semanticLabel: 'ADD',
),
onPressed: () {},
),


],
),
);

Widget price = new Row(
children: <Widget>[

new DecoratedBox(

decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(30.0)
),
child: new ClipRRect(
borderRadius: new BorderRadius.circular(50.0),
child: new MaterialButton(onPressed: null,
minWidth: 20.0,
color: Colors.grey[900],
child: new Text("€${modelList[0].price}", style: new TextStyle(
color: Colors.white,
fontSize: 17.0,
),),
),
),
),

],
);

Widget titleSection = new Container(
padding: const EdgeInsets.all(20.0),
child: new Row(
children: [
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new Container(

padding: const EdgeInsets.only(bottom: 8.0),
child: new Text(modelList[0].name, style: new TextStyle(
fontSize: 20.9,
fontWeight: FontWeight.bold
),),
),
//new Text("Corn Pizza"),
price
]
)),


new Expanded(child: new Column(
children: <Widget>[
// price,
addRemoveButtonRedux
],
))
],
),
);

Widget description = new Container(

//padding: const EdgeInsets.all(20.0),
child: new Column(
children: [

new Card(

child: new Container(
width: 400.0,
height: 200.0,
padding: const EdgeInsets.all(5.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[

new Text("Description: ", style: new TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold
),),
new Padding(padding: const EdgeInsets.only
(bottom: 20.0)),
isDesription ? new Text("Description not available") : new Text(
modelList[0].description),
],
),
)
)


]

));
modelList = snapshot.data;
//print("Snapshot: "+modelList[0].description);
//print("Snapshot: "+modelList[0].id.toString());
//print("Snapshot: "+modelList[0].price.toString());



List<ImageProvider> networkimages = <ImageProvider>[];

for(int i=0 ; i< modelList[0].listThumbnails.length; i++ )
{
networkimages.add(new NetworkImage(
CommonMethods.image_url+modelList[0].listThumbnails[i]
)
);
}

return new Container(
child: new ListView(
children: [

new ImageCarousel(
networkimages,
interval: new Duration(seconds: 5),
showCloseButtonOnZoom: true,

),




titleSection,
description,

]
)
);
}

else
{
// print("ERROR");
return new Center(
child: new Text("Loading Data....",
style: new TextStyle(fontSize: 20.0),),
);
}
});
}

这里是 Increment 和 Decrement 方法。

decrement() {
setState(() {
quantity--;
});

}

increment(callback) {
setState(() {
quantity++;

Product product = new Product(modelList[0].name,
modelList[0].price,
modelList[0].id,
modelList[0].listThumbnails[0]);
CartItem cartItem = new CartItem(
product,
quantity
);
callback(cartItem);

});

}

回调方法:

typedef OnItemAddedCallback = Function(CartItem itemName);

最佳答案

我已经在我的 reducer.dart 类中使用这段代码解决了这个问题。

List<CartItem> addItem(List<CartItem> items, AddItemAction action)
{
bool isItemFound=false;
if(items!=null)
{

for(int itemcount=0;itemcount<items.length;itemcount++){
CartItem item=items[itemcount];
if(item.product.id==action.item.product.id){
print("itemcount found");
item.quantity=action.item.quantity;
isItemFound=true;
break;
}
}

}else{
items=new List();
}

if(!isItemFound){
items.add(action.item);
}

return items;
}

关于dart - Flutter 更新购物车项目列表同时删除重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51081470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com