18.1 下单界面编写
实现步骤:
第 1 步:i18n
lib/common/i18n/locale_keys.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| static const placeOrderTitle = "place_order_title"; static const placeOrderPayment = "place_order_payment"; static const placeOrderShippingAddress = "place_order_shipping_address"; static const placeOrderQuantity = "place_order_quantity"; static const placeOrderPrice = "place_order_price"; static const placeOrderPriceShipping = "place_order_price_shipping"; static const placeOrderPriceDiscount = "place_order_price_discount"; static const placeOrderPriceVoucherCode = "place_order_price_voucher_code"; static const placeOrderPriceVoucherCodeEnter = "place_order_price_voucher_code_enter"; static const placeOrderTotal = "place_order_total"; static const placeOrderBtnPlaceOrder = "place_order_btn_place_order";
static const orderConfirmationTitle = "order_confirmation_title"; static const orderConfirmationDesc = "order_confirmation_desc"; static const orderConfirmationBtnHome = "order_confirmation_btn_home";
|
lib/common/i18n/locales/locale_en.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| LocaleKeys.placeOrderTitle: 'Checkout', LocaleKeys.placeOrderPayment: 'Payment Method', LocaleKeys.placeOrderShippingAddress: 'Shipping Address', LocaleKeys.placeOrderQuantity: 'Quantity', LocaleKeys.placeOrderPrice: 'Price', LocaleKeys.placeOrderPriceShipping: 'Shipping', LocaleKeys.placeOrderPriceDiscount: 'Discount', LocaleKeys.placeOrderPriceVoucherCode: 'Voucher Code', LocaleKeys.placeOrderPriceVoucherCodeEnter: 'Voucher Code Enter', LocaleKeys.placeOrderTotal: 'Total', LocaleKeys.placeOrderBtnPlaceOrder: 'Place Order',
LocaleKeys.orderConfirmationTitle: 'Order Placed', LocaleKeys.orderConfirmationDesc: 'Your order was placed Successfully', LocaleKeys.orderConfirmationBtnHome: 'Go Home',
|
lib/common/i18n/locales/locale_zh.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| LocaleKeys.placeOrderTitle: '确认订单', LocaleKeys.placeOrderPayment: '支付方式', LocaleKeys.placeOrderShippingAddress: '送货地址', LocaleKeys.placeOrderQuantity: '数量', LocaleKeys.placeOrderPrice: '价格', LocaleKeys.placeOrderPriceShipping: '运费', LocaleKeys.placeOrderPriceDiscount: '折扣', LocaleKeys.placeOrderPriceVoucherCode: '代金券', LocaleKeys.placeOrderPriceVoucherCodeEnter: '输入代金券', LocaleKeys.placeOrderTotal: '小计', LocaleKeys.placeOrderBtnPlaceOrder: '下单确认',
LocaleKeys.orderConfirmationTitle: '已下订单', LocaleKeys.orderConfirmationDesc: '您的订单已成功下达', LocaleKeys.orderConfirmationBtnHome: '返回首页',
|
第 2 步:控制器
lib/pages/cart/buy_now/controller.dart
1 2
| final ProductModel product;
|
1 2 3 4 5 6 7
| List<String> paymentList = [ AssetsImages.pVisaPng, AssetsImages.pCashPng, AssetsImages.pMastercardPng, AssetsImages.pPaypalPng, ];
|
1 2
| void onCheckout() async {}
|
第 3 步:视图
lib/pages/cart/buy_now/view.dart
1 2
| final ProductModel product;
|
1 2 3 4
| const BuyNowPage({ Key? key, required this.product, }) : super(key: key);
|
1 2 3 4
| Widget _buildTitle(String text) { return TextWidget.title2(text).paddingBottom(AppSpace.listRow); }
|
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
| Widget _buildPayment() { return List<Widget>.generate( controller.paymentList.length, (index) => ImageWidget.asset( controller.paymentList[index], width: 106.w, height: 50.w, backgroundColor: AppColors.surface, ) .decorated( border: Border.all( color: AppColors.surfaceVariant, width: 0.5, ), borderRadius: BorderRadius.all(Radius.circular(AppRadius.button)), ) .paddingRight(AppSpace.iconTextSmail), ) .toList() .toListView( scrollDirection: Axis.horizontal, ) .height(50.w) .paddingBottom(AppSpace.listRow); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Widget _buildButtons() { return <Widget>[ ButtonWidget.secondary( LocaleKeys.commonBottomCancel.tr, onTap: () => Get.back(), ).expanded(),
SizedBox(width: AppSpace.iconTextLarge),
ButtonWidget.primary( LocaleKeys.placeOrderBtnPlaceOrder.tr, onTap: controller.onCheckout, ).expanded(), ].toRow( mainAxisAlignment: MainAxisAlignment.spaceAround, ); }
|
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
| Widget _buildView() { return <Widget>[ _buildTitle(LocaleKeys.placeOrderPayment.tr), _buildPayment(),
_buildTitle(LocaleKeys.placeOrderShippingAddress.tr),
_buildTitle(LocaleKeys.placeOrderQuantity.tr),
_buildTitle(LocaleKeys.placeOrderPrice.tr),
_buildButtons(),
] .toColumn( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, ) .paddingAll(AppSpace.page); }
|
1 2 3 4 5 6 7 8 9 10
| @override Widget build(BuildContext context) { return GetBuilder<BuyNowController>( init: BuyNowController(product), id: "buy_now", builder: (_) { return _buildView(); }, ); }
|
第 4 步:商品详情 “立刻下单”
控制器
lib/pages/goods/product_details/controller.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void onCheckoutTap() async { if (!await UserService.to.checkIsLogin()) { return; }
if (product == null || product?.id == null) { Loading.error("product is empty"); return; }
ActionBottomSheet.barModel( BuyNowPage(product: product!), ); }
|
ActionBottomSheet.barModel
采用底部弹出视图的方式展示
视图
lib/pages/goods/product_details/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Widget _buildButtons() { return controller.product == null ? const SizedBox.shrink() : <Widget>[ ...
ButtonWidget.primary( LocaleKeys.gDetailBtnBuy.tr, onTap: controller.onCheckoutTap, ).expanded(), ] .toRow( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceAround, ) .paddingHorizontal(AppSpace.page); }
|
提交代码到 git