哥哥带你Flutter实战五步走,第三步,状态管理
一
class DetailsInfoProvidewith ChangeNotifier {
DetailsModelgoodsInfo;
boolisLeft =true;
boolisRight =false;
//TabBar切换方法
changeLeftAndRight(String changeState) {
if (changeState =='left') {
isLeft =true;
isRight =false;
}else {
isLeft =false;
isRight =true;
}
notifyListeners();
}
//后台获取商品数据
getGoodsInfo(String id) {
var formData = {'goodId': id};
request('getGoodDetailById', formData: formData).then((value) {
var responseData = json.decode(value.toString());
if (responseData !=null) {
goodsInfo =DetailsModel.fromJson(responseData);
notifyListeners();
}
});
}
}
二
class CurrentIndexProvidewith ChangeNotifier{
intcurrentIndex =0;
changeIndex(int newIndex){
currentIndex = newIndex;
notifyListeners();
}
}
三
//混入
class Counterwith ChangeNotifier{
intvalue =0;
increment(){
value +=1;
//发送通知
NotificationListener();
}
}
四
//混入
class ChildCategorywith ChangeNotifier {
ListchildCategoryList = [];
intchildIndex =0; //高亮显示
StringcategoryId ='4'; //大类默认ID
StringsubId =''; //没有小类传空
intpage =1; //类表页数
StringnoMoreText =''; //无更多数据
//大类清零数据
getChildCategory(List list, String id) {
page =1;
noMoreText ='';
//往数组中添加元素
childIndex =0;
categoryId = id;
BxMallSubDto all =BxMallSubDto();
all.mallSubId ='';
all.mallCategoryId ='00';
all.comments ='null';
all.mallSubName ='全部';
childCategoryList = [all];
childCategoryList.addAll(list);
notifyListeners();
}
//改变子类索引
changeChildIndex(index, String id) {
page =1;
noMoreText ='';
childIndex = index;
subId = id;
notifyListeners();
}
//增加page
addPage() {
page +=1;
}
changeNoMoreData(String text) {
noMoreText = text;
notifyListeners();
}
}
五
//混入
class CategoryGoodsListProvidewith ChangeNotifier {
ListgoodsList = [];
//点击大类更换商品列表数据源
getGoodsList(List list) {
goodsList = list;
notifyListeners();
}
getMoreList(List list){
goodsList.addAll(list);
notifyListeners();
}
}
六
class CartProviderwith ChangeNotifier {
StringcartString ='[]';
ListcartList = [];
doubleallPrice =0.0; //商品总价
intallGoodsCount =0; //商品总数
boolisAllCheck =true; //是否全选
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
List tempList = (tempas List).cast();
//数量增加,商品不增加
bool isHave =false;
int ival =0;
allGoodsCount =0;
allPrice =0;
tempList.forEach((item) {
if (item['goodsId'] == goodsId) {
tempList[ival]['count'] = item['count'] +1;
cartList[ival].count++;
isHave =true;
}
if(item['isCheck']){
allPrice += (cartList[ival].price *cartList[ival].count);
allGoodsCount +=cartList[ival].count;
}
ival++;
});
if (!isHave) {
Map newGoods = {
'goodsId': goodsId,
'goodsName': goodsName,
'count': count,
'price': price,
'images': images,
'isCheck':true,
};
tempList.add(newGoods);
cartList.add(CartInfoMdel.fromJson(newGoods));
allPrice += (count*price);
allGoodsCount += count;
}
cartString = json.encode(tempList).toString();
prefs.setString('cartInfo', cartString);
print('model ===========${cartList}');
//添加通知,改变界面
notifyListeners();
}
remove()async {
SharedPreferences prefs =await SharedPreferences.getInstance();
prefs.remove('cartInfo');
print('清空完成===================');
notifyListeners();
}
getCartInfo()async {
SharedPreferences prefs =await SharedPreferences.getInstance();
cartString = prefs.getString('cartInfo');
cartList = [];
if (cartString ==null) {
cartList = [];
}else {
List tempList = (json.decode(cartString.toString())as List).cast();
allGoodsCount =0;
allPrice =0.0;
isAllCheck =true;
tempList.forEach((item) {
if (item['isCheck']) {
allPrice += (item['count'] * item['price']);
allGoodsCount += item['count'];
}else {
isAllCheck =false;
}
cartList.add(CartInfoMdel.fromJson(item));
});
}
notifyListeners();
}
//删除单个购物车商品
deleteOneGoods(String goodsId)async {
SharedPreferences prefs =await SharedPreferences.getInstance();
cartString = prefs.getString('cartInfo');
//转成map 类型
List tempList = (json.decode(cartString.toString())as List).cast();
int tempIndex =0;
int delIndex =0;
tempList.forEach((item) {
if (item['goodsId'] == goodsId) {
delIndex = tempIndex;
}
tempIndex++;
});
tempList.removeAt(delIndex);
cartString = json.encode(tempList).toString();
prefs.setString('cartInfo', cartString);
await getCartInfo();
}
//改变商品全选选择,
changeCheckState(CartInfoMdel cartItem)async {
SharedPreferences prefs =await SharedPreferences.getInstance();
cartString = prefs.getString('cartInfo');
//转成map 类型
List tempList = (json.decode(cartString.toString())as List).cast();
int tempIndex =0;
int changeIndex =0;
tempList.forEach((item) {
if (item['goodsId'] == cartItem.goodsId) {
changeIndex = tempIndex;
}
tempIndex++;
});
tempList[changeIndex] = cartItem.toJson();
cartString = json.encode(tempList).toString();
prefs.setString('cartInfo', cartString);
await getCartInfo();
}
//点击全选按钮操作
changeAllCheckBtnState(bool isCheck)async {
SharedPreferences prefs =await SharedPreferences.getInstance();
cartString = prefs.getString('cartInfo');
//转成map 类型
List tempList = (json.decode(cartString.toString())as List).cast();
List newList = [];
for (var itemin tempList) {
var newItem = item;
newItem['isCheck'] = isCheck;
newList.add(newItem);
}
cartString = json.encode(newList).toString();
prefs.setString('cartInfo', cartString);
await getCartInfo();
}
//商品加减
addOrReduceAction(var carItem, String todo)async {
SharedPreferences prefs =await SharedPreferences.getInstance();
cartString = prefs.getString('cartInfo');
//转成map 类型
List tempList = (json.decode(cartString.toString())as List).cast();
int tempIndex =0;
int changeIndex =0;
// print('123123');
tempList.forEach((item){
if(item['goodsId'] == carItem.goodsId){
changeIndex = tempIndex;
}
tempIndex ++;
});
if (todo =='add'){
carItem.count++;
}else if (carItem.count >1){
carItem.count--;
}
tempList[changeIndex] = carItem.toJson();
cartString = json.encode(tempList).toString();
prefs.setString('cartInfo', cartString);
await getCartInfo();
}
}