在删除重复条目的同时更新购物车项目列表

时间:2018-06-28 11:13:27

标签: dart e-commerce flutter cart

我正在使用购物车应用程序,并使用flutter_redux更新我的购物车。一切工作正常,但是我的购物车列表中有重复的物品,如何删除这些重复的条目并增加数量。我想获得包含物品和数量的清单。请通过这段代码为我提供帮助,在哪里可以添加逻辑并使用删除的重复项过滤列表。

这是我的代码:

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),),
        );
      }
    });
 }

这是递增和递减方法。

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);

1 个答案:

答案 0 :(得分:0)

我使用我的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;
}