coupon_provider.dart
1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
}
}