主要做购物车后面的删除按钮

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

 

删除的方法写在provide里面

provide/cart.dart文件

传入goodsId,循环对比,找到后进行移除

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

 

  //删除单个购物车商品
  deleteOneGoods(String goodsId) async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
    cartString=prefs.getString('cartInfo');
    List<Map> tempList=(json.decode(cartString.toString()) as List).cast();
    int tempIndex=0;//定义循环的索引
    int deleteIndex=0;//要删除的索引
    tempList.forEach((item){
      if(item['goodsId']==goodsId){
        deleteIndex=tempIndex;
      }
      tempIndex++;
    });
    tempList.removeAt(deleteIndex);//删除
    //删除后转换成string进行持久化
    cartString=json.encode(tempList.toString());//list转字符串
    prefs.setString('cartInfo', cartString);
    await getCartInfo();//重新获取下列表数据,因为getCartInfo方法里面有通知,这里就不再调用了
  }

 

ui内增加删除的事件

cart_page.dart文件内,我们需要在ListView的外层套一层Provide组件

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

 

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

这样我们购物车的数据就是动态的了

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

删除的事件-cart_item.dart

先引入cartProvide和provide

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

传入上下文context,和item当前这个商品对象

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

 

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

 

运行程序查看效果

 

修正一个错误的地方,应该是先encode再去toString()

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

Flutter实战视频-移动电商-58.购物车_删除商品功能制作

最终代码

provide/cart.dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import '../model/cartInfo.dart';

class CartProvide with ChangeNotifier{
  String cartString="[]";//声明一个变量 做持久化的存储
  List<CartInfoModel> cartList=[];

  //声明一个异步的方法,购物车操作放在前台不在请求后台的数据
  save(goodsId,goodsName,count,price,images) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString= prefs.getString('cartInfo');//先从持久化中获取
    var temp = cartString==null?[]:json.decode(cartString.toString());
    //声明list 强制类型是Map
    List<Map> tempList=(temp as List).cast();//把temp转成list
    bool isHave=false;//是否已经存在了这条记录
    int ival=0;//foreach循环的索引
    //循环判断列表是否存在该goodsId的商品,如果有就数量+1
    tempList.forEach((item){
      if(item['goodsId']==goodsId){
        tempList[ival]['count']=item['count']+1;
        cartList[ival].count++;
        isHave=true;
      }
      ival++;
    });
    //没有不存在这个商品,就把商品的json数据加入的tempList中
    if(!isHave){
      Map<String,dynamic> newGoods={
        'goodsId':goodsId,//传入进来的值
        'goodsName':goodsName,
        'count':count,
        'price':price,
        'images':images,
        'isCheck':true
      };
      tempList.add(newGoods);
      cartList.add(CartInfoModel.fromJson(newGoods));
    }
    cartString=json.encode(tempList).toString();//json数据转字符串
    // print('字符串》》》》》》》》》》》${cartString}');
    // print('字符串》》》》》》》》》》》${cartList}');

    prefs.setString('cartInfo', cartString);
    notifyListeners();
  }
  remove() async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
    prefs.remove('cartInfo');
    cartList=[];
    print('清空完成----------------------');
    notifyListeners();
  }

  getCartInfo() async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
    cartString=prefs.getString('cartInfo');//持久化中获得字符串
    print('购物车持久化的数据================>'+cartString);
    cartList=[];//把最终的结果先设置为空list
    if(cartString==null){
      cartList=[];//如果持久化内没有数据 那么就还是空的list
    }else{
      //声明临时的变量
      List<Map> tempList=(json.decode(cartString.toString()) as List).cast();
      tempList.forEach((item){
        cartList.add(CartInfoModel.fromJson(item));//json转成对象,加入到cartList中
      });
      
    }
    notifyListeners();//通知
  }

  //删除单个购物车商品
  deleteOneGoods(String goodsId) async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
    cartString=prefs.getString('cartInfo');
    List<Map> tempList=(json.decode(cartString.toString()) as List).cast();
    int tempIndex=0;//定义循环的索引
    int deleteIndex=0;//要删除的索引
    tempList.forEach((item){
      if(item['goodsId']==goodsId){
        deleteIndex=tempIndex;
      }
      tempIndex++;
    });
    tempList.removeAt(deleteIndex);//删除
    //删除后转换成string进行持久化
    cartString=json.encode(tempList).toString();//list转字符串
    prefs.setString('cartInfo', cartString);
    await getCartInfo();//重新获取下列表数据,因为getCartInfo方法里面有通知,这里就不再调用了
  }


}
View Code

相关文章: