18.3 数量修改, 小计栏位
实现步骤:
第 1 步:控制器
lib/pages/cart/buy_now/controller.dart
1 2 3 4 5 6 7 8
| int quantity = 1;
double get shipping => 0;
double get discount => 0;
double get totalPrice => double.parse(product.price!) * quantity;
|
1 2 3 4 5 6 7 8
| void onQuantityChange(int value) { if (value <= 0) { value = 1; } quantity = value; update(["buy_now"]); }
|
第 2 步:价格行组件
lib/pages/cart/buy_now/widgets/price_line.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
| import 'package:flutter/material.dart'; import 'package:flutter_woo_commerce_getx_learn/common/index.dart';
class BuildPriceLine extends StatelessWidget { const BuildPriceLine({ Key? key, this.titleString, this.priceString, this.leftWidget, this.rightWidget, }) : super(key: key);
final String? titleString;
final String? priceString;
final Widget? leftWidget;
final Widget? rightWidget;
Widget _buildView() { return <Widget>[ leftWidget?.expanded() ?? TextWidget.body2(titleString ?? "").expanded(),
rightWidget ?? TextWidget.body2(priceString ?? ""), ].toRow().paddingBottom(AppSpace.listRow); }
@override Widget build(BuildContext context) { return _buildView(); } }
|
第 3 步:视图
lib/pages/cart/buy_now/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
| Widget _buildPrice() { return <Widget>[ BuildPriceLine( titleString: LocaleKeys.placeOrderPriceShipping.tr, priceString: "\$${controller.shipping}", ),
BuildPriceLine( titleString: LocaleKeys.placeOrderPriceDiscount.tr, priceString: "\$${controller.discount}", ),
BuildPriceLine( titleString: LocaleKeys.placeOrderPriceVoucherCode.tr, rightWidget: ButtonWidget.text( LocaleKeys.placeOrderPriceVoucherCodeEnter.tr, textSize: 9, textColor: AppColors.highlight, ), ),
BuildPriceLine( leftWidget: TextWidget.body1(LocaleKeys.placeOrderTotal.tr), rightWidget: TextWidget.body1( "\$${controller.totalPrice - controller.discount}"), ),
].toColumn().paddingBottom(AppSpace.listRow); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Widget _buildView() { return <Widget>[ ...
_buildTitle(LocaleKeys.placeOrderQuantity.tr), QuantityWidget( quantity: controller.quantity, onChange: controller.onQuantityChange, ).paddingBottom(AppSpace.listRow),
_buildTitle(LocaleKeys.placeOrderPrice.tr), _buildPrice(),
|
提交代码到 git