首页 > 解决方案 > 编辑现有列表物品

问题描述

我想编辑列表,但我不知道如何实现。

我在下面粘贴了我的代码

Container(
              child: ListView.builder(

                  itemCount: filterList.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int intex) {
                  //  return MenuList(filterList[intex]);

                    return Container
                      (
                      height:75,
                      //color: Colors.blue,
                      child: Card(
                        elevation: 1.0,
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.0)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              child: Row(
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.only(left:8.0),
                                    child: Container(
                                      child: Image(image:AssetImage("images/beef_burger.jpg"),
                                        width: 70,height: 50,),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                      child: Column(

                                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: <Widget>[

                                          Container(
                                            width: MediaQuery.of(context).size.width/2.2,
                                            //  color: Colors.blue,
                                            child: Text(filterList[intex].itemName,maxLines: 1,),
                                          ),
                                          Container(
                                            child: Text("AED ${filterList[intex].itemPrice}",style: TextStyle(color: Colors.grey),),
                                          ),
                                        ],
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),

                            Container(
                              // color: Colors.red,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,

                                children: <Widget>[
                                  Container(
                                    width: 50,
                                    height: 50,
                                    // color: Colors.blue,
                                    child: FlatButton(onPressed: (){

                                      if(filterList[intex].counter > 0){
                                        setState(() {
                                          filterList[intex].counter--;
                                        });

                                      }

                                    }, child: Image(image: AssetImage("images/minus.png"),width: 20,height: 20,),),
                                  ),
                                  Container(
                                    child: Text("${filterList[intex].counter}"),
                                  ),
                                  Container(
                                    width: 50,
                                    height: 50,
                                    //  color: Colors.green,
                                    child: FlatButton(onPressed: () async{

                                      setState(() {
                                        filterList[intex].counter++;
                                      });



                                      cartItemList.add({"itemObj":filterList[intex],"quantity":filterList[intex].counter});



                                      //  cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];

                                      print(addedItems(cartItemList));

                                      final prefs = await SharedPreferences.getInstance();
                                      await prefs.setStringList('itemList', addedItems(cartItemList));

                                    }, child: Image(image: AssetImage("images/plus.png"),width: 20,height: 20,),),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),

                    );

                  }),
            ),

我已将列表转换为列表以存储在共享首选项中

List<String> addedItems(List<dynamic> cartList){


 try {
  var res = cartList.map((v) => 
  json.encode(v)).toList();
  listNew = res;
   return res;
   } catch (err) {
   // Just in case
   return [];
   }

当我将项目添加到购物车列表时,我得到的输出为

  1. 添加了一项

[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"数量":1}]


  1. 再次添加相同的项目

[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"quantity":1}, {"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001" ,"item_discount":0.0,"item_id":552,"category_id":12},"quantity":2}]


  1. 添加了不同的项目

[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"quantity":1}, {"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001" ,"item_discount":0.0,"item_id":552,"category_id":12},"quantity":2}, {"itemObj":{"item_price":20.0,"item_name":"英式早餐","item_img ":" ","item_code":"002","item_discount":0.0,"item_id":71,"category_id":12},"quantity":1}]


但我想要输出,就像我再次添加相同的项目时一样

[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552, "category_id":12},"数量":2}]


标签: listflutterdart

解决方案


List 没有内置功能,只能添加独特的项目。您可以使用地图或手动检查该项目是否已存在。

要检查您可以使用 indexWhere 方法: https ://api.flutter.dev/flutter/dart-core/List/indexWhere.html


推荐阅读