12.6 颜色分组组件编写
实现步骤:
第 1 步:离线缓存常量
lib/common/values/constants.dart
1 2
| static const storageProductsAttributesColors = 'products_attributes_colors';
|
第 2 步:定义模型
slug
是颜色值
name
是显示的名称
生成定义json to dart
lib/common/models/woo/attribute_model/attribute_model.dart
第 3 步:API 定义
lib/common/api/product.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
static Future<List<AttributeModel>> attributes(int id) async { var res = await WPHttpService.to.get( '/products/attributes/$id/terms', );
List<AttributeModel> attributes = []; for (var item in res.data) { attributes.add(AttributeModel.fromJson(item)); } attributes.sort((a, b) => a.menuOrder!.compareTo(b.menuOrder as int)); return attributes; }
|
第 4 步:拉取颜色定义
我们再商品首页拉取数据并缓存
lib/pages/goods/home/controller.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Future<void> _initData() async { ... var attributeColors = await ProductApi.attributes(1);
Storage().setJson(Constants.storageProductsAttributesColors, attributeColors);
...
update(["home"]); }
|
第 5 步:颜色分组组件编写
lib/common/components/colors_list.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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import 'package:flutter/material.dart';
import '../index.dart';
class ColorsListWidget extends StatelessWidget { final Function(List<String> keys)? onTap;
final List<KeyValueModel<AttributeModel>> itemList;
final List<String> keys;
final double? size;
final Color? borderSelectedColor;
final double? spacing;
final double? runSpacing;
const ColorsListWidget({ Key? key, this.onTap, required this.itemList, required this.keys, this.size, this.spacing, this.runSpacing, this.borderSelectedColor, }) : super(key: key);
@override Widget build(BuildContext context) { return <Widget>[ for (var item in itemList) SizedBox( width: size ?? 24, height: size ?? 24, ) .decorated( color: item.value.slug?.toColor, border: keys.hasValue(item.key) == true ? Border.all( color: borderSelectedColor ?? AppColors.highlight, width: 2, ) : null, borderRadius: BorderRadius.circular(size ?? 12), ) .tight(width: size, height: size) .onTap(() { if (keys.hasValue(item.key)) { keys.remove(item.key); } else { keys.add(item.key); } onTap?.call(keys); }) ].toWrap( spacing: spacing ?? AppSpace.listItem, runSpacing: runSpacing ?? AppSpace.listRow, ); } }
|
第 6 步: 控制器 - 读取缓存
lib/pages/goods/product_details/controller.dart
1 2 3 4
| List<KeyValueModel<AttributeModel>> colors = [];
List<String> colorKeys = [];
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| _loadCache() async { var stringColors = Storage().getString(Constants.storageProductsAttributesColors);
colors = stringColors != "" ? jsonDecode(stringColors).map<KeyValueModel<AttributeModel>>((item) { var arrt = AttributeModel.fromJson(item); return KeyValueModel(key: "${arrt.name}", value: arrt); }).toList() : []; }
|
1 2 3 4 5
| void onColorTap(List<String> keys) { colorKeys = keys; update(["product_colors"]); }
|
1 2 3 4
| _initData() async { await _loadCache(); ... }
|
第 7 步:视图 - 规格页使用
lib/pages/goods/product_details/widgets/tab_product.dart
1 2 3 4
| _buildTitle(String title) { return TextWidget.body1(title).paddingBottom(AppSpace.listRow); }
|
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
| @override Widget build(BuildContext context) { return <Widget>[ ...
_buildTitle("Color"), GetBuilder<ProductDetailsController>( id: "product_colors", tag: uniqueTag, builder: (_) { return ColorsListWidget( itemList: controller.colors, keys: controller.colorKeys, size: 33.w, onTap: controller.onColorTap, ).paddingBottom(AppSpace.listRow * 2); }, ), ] .toColumn( crossAxisAlignment: CrossAxisAlignment.start, ) .paddingVertical(AppSpace.page);
... }
|
提交代码到 git