18.4 优惠券栏位
 
实现步骤:
第 1 步:i18n 多语言
lib/common/i18n/locale_keys.dart
| 12
 3
 4
 
 | static const promoCode = "promo_code";
 static const promoDesc = "promo_desc";
 static const promoEnterCodeTip = "promo_enter_code_tip";
 
 | 
lib/common/i18n/locales/locale_en.dart
| 12
 3
 4
 5
 
 | LocaleKeys.promoCode: 'Apply Promo Code',
 LocaleKeys.promoDesc:
 'Promo Code is simply dummy text the printing and typesetting industry',
 LocaleKeys.promoEnterCodeTip: 'Enter code here',
 
 | 
lib/common/i18n/locales/locale_zh.dart
| 12
 3
 4
 
 | LocaleKeys.promoCode: '使用优惠码',
 LocaleKeys.promoDesc: '促销代码只是印刷和排版行业的虚拟文本',
 LocaleKeys.promoEnterCodeTip: '输入代码',
 
 | 
第 2 步:优惠券页面
控制器
lib/pages/cart/apply_promo_code/controller.dart
| 12
 
 | TextEditingController couponController = TextEditingController();
 
 | 
| 12
 3
 4
 5
 
 | @overridevoid onClose() {
 super.onClose();
 couponController.dispose();
 }
 
 | 
视图
lib/pages/cart/apply_promo_code/view.dart
| 12
 
 | final Function(String) onApplyCouponCode;
 
 | 
| 12
 3
 4
 
 | const ApplyPromoCodePage({Key? key,
 required this.onApplyCouponCode,
 }) : super(key: key);
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | Widget _buildButtons() {
 return <Widget>[
 
 ButtonWidget.text(
 LocaleKeys.commonBottomCancel.tr,
 onTap: () => Get.back(),
 ),
 
 
 ButtonWidget.text(
 LocaleKeys.commonBottomApply.tr,
 
 onTap: () {
 
 onApplyCouponCode(controller.couponController.text);
 Get.back();
 },
 textColor: AppColors.highlight,
 textWeight: FontWeight.w500,
 ),
 ].toRow(
 mainAxisAlignment: MainAxisAlignment.end,
 );
 }
 
 | 
| 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
 
 | Widget _buildView() {
 return <Widget>[
 
 TextWidget.title3(LocaleKeys.promoCode.tr)
 .paddingBottom(AppSpace.listRow),
 
 
 TextWidget.body2(
 LocaleKeys.promoDesc.tr,
 maxLines: 3,
 softWrap: true,
 ).paddingBottom(AppSpace.listRow),
 
 
 InputWidget.textBorder(
 controller: controller.couponController,
 hintText: "Enter your coupon code",
 ).paddingBottom(AppSpace.listRow),
 
 
 _buildButtons(),
 ]
 .toColumn(
 crossAxisAlignment: CrossAxisAlignment.start,
 mainAxisSize: MainAxisSize.min,
 )
 .paddingAll(40)
 .backgroundColor(AppColors.background);
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | @overrideWidget build(BuildContext context) {
 return GetBuilder<ApplyPromoCodeController>(
 init: ApplyPromoCodeController(),
 id: "apply_promo_code",
 builder: (_) {
 return _buildView();
 },
 );
 }
 
 | 
第 3 步:控制器
lib/pages/cart/buy_now/controller.dart
| 12
 3
 4
 5
 6
 7
 8
 
 | final List<CouponsModel> lineCoupons = [];
 
 
 double get discount =>
 lineCoupons.fold<double>(0, (double previousValue, CouponsModel element) {
 return previousValue + (double.parse(element.amount ?? "0"));
 });
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | bool _applyCoupon(CouponsModel item) {
 
 int index = lineCoupons.indexWhere((element) => element.id == item.id);
 if (index >= 0) {
 return false;
 }
 
 lineCoupons.add(item);
 return true;
 }
 
 | 
| 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
 
 | void onEnterCouponCode() {
 ActionBottomSheet.popModal(
 child: ApplyPromoCodePage(
 onApplyCouponCode: (couponCode) async {
 
 if (couponCode.isEmpty) {
 Loading.error("Voucher code empty.");
 return;
 }
 CouponsModel? coupon = await CouponApi.couponDetail(couponCode);
 if (coupon != null) {
 couponCode = "";
 bool isSuccess = _applyCoupon(coupon);
 if (isSuccess) {
 Loading.success("Coupon applied.");
 } else {
 Loading.error("Coupon is already applied.");
 }
 update(["buy_now"]);
 } else {
 Loading.error("Coupon code is not valid.");
 }
 },
 ),
 safeAreaMinimum: EdgeInsets.fromLTRB(50.w, 0, 50.w, 140.w),
 );
 }
 
 | 
第 4 步:视图
lib/pages/cart/buy_now/view.dart
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | Widget _buildPrice() {
 ...
 
 
 BuildPriceLine(
 titleString: LocaleKeys.placeOrderPriceVoucherCode.tr,
 rightWidget: ButtonWidget.text(
 ...
 onTap: controller.onEnterCouponCode,
 ),
 ),
 
 ...
 
 | 
优惠券: 568935ab
提交代码到 git