17.3 购物车数据列表
实现步骤:
第 1 步:阿里图片尺寸调整
lib/common/utils/convert.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Convert { static String aliImageResize( String url, { double width = 300, double? maxHeight, }) { var crop = ''; int _width = width.toInt(); int? _maxHeight = maxHeight?.toInt();
if (maxHeight != null) { crop = '/crop,h_$_maxHeight,g_center'; } return url + '?x-oss-process=image/resize,w_$_width,m_lfit$crop'; } }
|
oss 图片加工 https://help.aliyun.com/document_detail/101260.html
oss 图片参数 https://help.aliyun.com/document_detail/44688.html
第 2 步:编写数据项组件
lib/pages/cart/cart_index/widgets/cart_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 92 93 94 95 96 97 98
| import 'package:flutter/cupertino.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_woo_commerce_getx_learn/common/index.dart'; import 'package:get/get.dart';
import '../index.dart';
class CartItem extends StatelessWidget { final LineItem lineItem;
final Function(int)? onChangeQuantity;
final Function(bool)? onSelect;
const CartItem({ Key? key, required this.lineItem, this.onChangeQuantity, this.onSelect, }) : super(key: key);
Widget _buildView() { ProductModel product = lineItem.product!;
return <Widget>[ ImageWidget.url( Convert.aliImageResize( product.images?.first.src ?? "", width: 100.w, ), fit: BoxFit.cover, width: 78.w, height: 100.w, radius: AppRadius.image.w, ).paddingRight(AppSpace.iconTextSmail),
<Widget>[ TextWidget.title3(lineItem.name ?? "").paddingBottom(AppSpace.listRow),
if (product.attributes?.isNotEmpty == true) TextWidget.body2( "${product.attributes?.first.name} - ${product.attributes?.first.options} ", ),
if (product.attributes?.length == 2) TextWidget.body2( "${product.attributes?[1].name} - ${product.attributes?[1].options} ", ),
<Widget>[ TextWidget.body2( "\$ ${lineItem.total}", weight: FontWeight.bold, ).expanded(),
].toRow().paddingTop(AppSpace.listRow),
] .toColumn( crossAxisAlignment: CrossAxisAlignment.start, ) .expanded(),
].toRow(); }
@override Widget build(BuildContext context) { return GetBuilder<CartIndexController>( id: "goods_${lineItem.productId}", builder: (controller) { return _buildView(); }); } }
|
第 3 步:视图页
lib/pages/cart/cart_index/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Widget _buildOrders() { return ListView.separated( itemBuilder: (BuildContext context, int index) { LineItem item = CartService.to.lineItems[index]; return CartItem( lineItem: item, ).paddingAll(AppSpace.card).card(); }, separatorBuilder: (BuildContext context, int index) { return SizedBox(height: AppSpace.listRow); }, itemCount: CartService.to.lineItems.length, ); }
|
提交代码到 git