9.6 商品列表项封装
我们的目标是很多地方能复用这个商品列表项,看着很有规律
由 主图、标题、价格 三个组成,然后自适应宽高
 
 
 
实现步骤:
 
lib/common/components/product_item.dart
| 12
 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,
 )
 
 .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