14.8 过滤面板 - 品牌、性别、新旧

14.8 过滤面板 - 品牌、性别、新旧

img

实现步骤:


第 1 步:定义常量

lib/common/values/constants.dart

1
2
3
4
5
6
7
// 品牌
static const storageProductsAttributesBrand = 'products_attributes_brand';
// 性别
static const storageProductsAttributesGender = 'products_attributes_gender';
// 新旧
static const storageProductsAttributesCondition =
'products_attributes_condition';

第 2 步:缓存数据

lib/pages/goods/home/controller.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
_initData() async {
...

// 品牌
var attributeBrand = await ProductApi.attributes(3);
// 性别
var attributeGender = await ProductApi.attributes(4);
// 新旧
var attributeCondition = await ProductApi.attributes(5);

// 保存离线数据
Storage().setString(
Constants.storageProductsAttributesBrand, jsonEncode(attributeBrand));
Storage().setString(
Constants.storageProductsAttributesGender, jsonEncode(attributeGender));
Storage().setString(Constants.storageProductsAttributesCondition,
jsonEncode(attributeCondition));

...

第 3 步:控制器

lib/pages/search/search_filter/controller.dart

1
2
3
4
5
6
7
8
9
10
11
// Brand
List<KeyValueModel<AttributeModel>> brands = [];
List<String> brandKeys = [];

// Gender
List<KeyValueModel<AttributeModel>> genders = [];
List<String> genderKeys = [];

// Condition
List<KeyValueModel<AttributeModel>> conditions = [];
List<String> conditionKeys = [];
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
// 读取缓存
void _loadCache() async {
...

// 品牌列表
{
String result =
Storage().getString(Constants.storageProductsAttributesBrand);
brands = jsonDecode(result).map<KeyValueModel<AttributeModel>>((item) {
var arrt = AttributeModel.fromJson(item);
return KeyValueModel(key: "${arrt.name}", value: arrt);
}).toList();
}

// 性别列表
{
String result =
Storage().getString(Constants.storageProductsAttributesGender);
genders = jsonDecode(result).map<KeyValueModel<AttributeModel>>((item) {
var arrt = AttributeModel.fromJson(item);
return KeyValueModel(key: "${arrt.name}", value: arrt);
}).toList();
}

// 新旧列表
{
String result =
Storage().getString(Constants.storageProductsAttributesCondition);
conditions =
jsonDecode(result).map<KeyValueModel<AttributeModel>>((item) {
var arrt = AttributeModel.fromJson(item);
return KeyValueModel(key: "${arrt.name}", value: arrt);
}).toList();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 品牌选中
void onBrandTap(List<String> keys) {
brandKeys = keys;
update(["filter_brands"]);
}

// 性别选中
void onGenderTap(List<String> keys) {
genderKeys = keys;
update(["filter_genders"]);
}

// 新旧选中
void onConditionTap(List<String> keys) {
conditionKeys = keys;
update(["filter_conditions"]);
}

第 4 步:FilterView 视图

lib/pages/search/search_filter/widgets/filter_view.dart

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
// 品牌选择
Widget _buildBrands() {
return GetBuilder<SearchFilterController>(
id: "filter_brands",
builder: ( _) {
return TagsListWidget(
onTap: controller.onBrandTap,
itemList: controller.brands,
keys: controller.brandKeys,
bgSelectedColor: AppColors.highlight,
textSelectedColor: AppColors.onPrimary,
borderRadius: 11,
height: 17,
width: 55,
textSize: 9,
textWeight: FontWeight.w400,
).paddingBottom(AppSpace.listRow * 2);
},
);
}

// 性别选择
Widget _buildGenders() {
return GetBuilder<SearchFilterController>(
id: "filter_genders",
builder: ( _) {
return TagsListWidget(
onTap: controller.onGenderTap,
itemList: controller.genders,
keys: controller.genderKeys,
bgSelectedColor: AppColors.highlight,
textSelectedColor: AppColors.onPrimary,
borderRadius: 11,
height: 17,
width: 55,
textSize: 9,
textWeight: FontWeight.w400,
).paddingBottom(AppSpace.listRow * 2);
},
);
}

// 新旧选择
Widget _buildConditions() {
return GetBuilder<SearchFilterController>(
id: "filter_conditions",
builder: ( _) {
return TagsListWidget(
onTap: controller.onConditionTap,
itemList: controller.conditions,
keys: controller.conditionKeys,
bgSelectedColor: AppColors.highlight,
textSelectedColor: AppColors.onPrimary,
borderRadius: 11,
height: 17,
width: 55,
textSize: 9,
textWeight: FontWeight.w400,
).paddingBottom(AppSpace.listRow * 2);
},
);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Widget _buildView() {
return <Widget>[
...

// 品牌
_buildTitle(LocaleKeys.searchFilterBrand.tr),
_buildBrands(),

// 性别
_buildTitle(LocaleKeys.searchFilterGender.tr),
_buildGenders(),

// 新旧
_buildTitle(LocaleKeys.searchFilterCondition.tr),
_buildConditions(),

提交代码到 git