9.6 商品列表项封装

9.6 商品列表项封装

我们的目标是很多地方能复用这个商品列表项,看着很有规律
由 主图、标题、价格 三个组成,然后自适应宽高

img img img

实现步骤:


ProductItemWidget 产品项业务组件

img

lib/common/components/product_item.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
80
81
82
83
84
85
86
87
88
89
90
91
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../index.dart';

/// 商品展示项
class ProductItemWidget extends StatelessWidget {
/// 点击事件
final Function()? onTap;

/// 商品数据模型
final ProductModel product;

/// 图片宽
final double? imgWidth;

/// 图片高
final double? imgHeight;

const ProductItemWidget(
this.product, {
Key? key,
this.onTap,
this.imgWidth,
this.imgHeight,
}) : super(key: key);

Widget _buildView(BoxConstraints constraints) {
var ws = <Widget>[
// 图片
if (product.images?.isNotEmpty == true)
ImageWidget.url(
product.images?.first.src ?? "",
fit: BoxFit.cover,
width: imgWidth ?? constraints.minWidth,
height: imgHeight,
),

// 描述
<Widget>[
// 标题
TextWidget.body2(product.name ?? ""),

// 价格
if (product.price != null)
TextWidget.body2(
product.price ?? "",
weight: FontWeight.bold,
),
]
.toColumn(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
)
.paddingHorizontal(5)
.expanded(),
];

return ws
.toColumn(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
)
// .backgroundColor(AppColors.onPrimary)
.card(
blurRadius: 5,
)
.padding(all: 5)
.onTap(() {
if (onTap != null) {
onTap?.call();
} else {
Get.toNamed(
RouteNames.goodsProductDetails,
arguments: {
"id": product.id,
},
);
}
});
}

@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return _buildView(constraints);
},
);
}
}

提交代码到 git