coupon_provider.dart 1.87 KB
import 'package:flutter/material.dart';
import 'package:one_key/constant/state.dart';

class CouponProvider with ChangeNotifier {
  List<bool> swichStateData = []; //优惠券是否启用  true启用   false停用
  PageState pageState = PageState.Loading;
  PageState pageStateHistory = PageState.Loading;
  PageState pageStateSelect = PageState.Loading;

  bool checkValue = true; //优惠券页面“全选”按钮状态 ----- 数据状态全部选中为true
  List<Map<String, dynamic>> listData = []; //优惠券页面数据
  List coupons = []; //选中的优惠券id集合

  CouponProvider() {
    for (int i = 0; i < 20; i++) {
      swichStateData.add(true);
      listData.add({'checked': true, 'id': '${i}'});
    }
  }

  //全选按钮执行“全选”
  addAll() async {
    //把数据状态全部改成true,即选中状态
    coupons = [];
    for (var i = 0; i < listData.length; i++) {
      listData[i]['checked'] = true;
      coupons.add(listData[i]['id']);
    }
    checkValue = true;
    notifyListeners();
  }

//全选按钮执行“全不选”
  cancelAll() async {
    //把数据状态全部改成false,即不选中状态
    for (var i = 0; i < listData.length; i++) {
      listData[i]['checked'] = false;
    }
    coupons = [];
    checkValue = false;
    notifyListeners();
  }

//选中
  addData(int index) {
    listData[index]['checked'] = true;
    coupons.add(listData[index]['id']);
    notifyListeners();
  }

//未选中
  cancelData(int index) {
    listData[index]['checked'] = false;
    coupons.removeWhere((element) => element == listData[index]['id']);
    notifyListeners();
  }

  //获取全选按钮的状态  true  or  false
  getCheckValue() {
    bool temp = listData.any((value) {
      return value['checked'] == false;
    });
    if (temp == true) {
      checkValue = false;
    } else {
      checkValue = true;
    }
    notifyListeners();
  }
}