18.5 下单成功界面
实现步骤:
第 1 步:api 接口
接口地址 post /orders
数据格式
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
| { "billing": { "first_name": "ducafe", "last_name": "cat", "company": "", "address_1": "bj", "address_2": "", "city": "bj", "state": "CN2", "postcode": "100126", "country": "CN", "email": "ducafecat1@gmail.com", "phone": "1231232132" }, "shipping": { "first_name": "ducafe", "last_name": "cat", "company": "", "address_1": "bj", "address_2": "", "city": "bj", "state": "CN2", "postcode": "100126", "country": "CN", "phone": "" }, "line_items": [ { "product_id": 13, "quantity": 1 } ] "coupon_lines": [{"code":"123"}] }
|
line_items
是你的商品,可以多个
billing
下单人
shipping
收货人
coupon_lines
优惠券
接口文件
lib/common/api/order.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
| import '../index.dart';
class OrderApi { static Future<OrderModel> crateOrder({ required List<LineItem> lineItem, required List<CouponsModel> lineCoupons, }) async { var data = { "line_items": lineItem .map((e) => {"product_id": e.productId!, "quantity": e.quantity!}) .toList(),
"coupon_lines": lineCoupons.map((e) => {"code": e.code!}).toList(),
"billing": UserService.to.profile.billing!.toJson(),
"shipping": UserService.to.profile.shipping!.toJson(), }; var res = await WPHttpService.to.post( '/orders', data: data, ); return OrderModel.fromJson(res.data); } }
|
第 2 步:完成界面
lib/pages/cart/buy_done/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
| Widget _buildView() { return <Widget>[ ImageWidget.asset( AssetsImages.orderConfirmedPng, height: 300.w, ).paddingBottom(40.w),
TextWidget.title2(LocaleKeys.orderConfirmationTitle.tr) .paddingBottom(10.w), TextWidget.body1(LocaleKeys.orderConfirmationDesc.tr).paddingBottom(50.w),
ButtonWidget.primary( LocaleKeys.commonBottomBack.tr, onTap: () => Get.back(), ).tight( width: 160.w, height: 50.w, ), ] .toColumn( mainAxisAlignment: MainAxisAlignment.center, ) .center(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @override Widget build(BuildContext context) { return GetBuilder<BuyDoneController>( init: BuyDoneController(), id: "buy_done", builder: (_) { return Scaffold( body: SafeArea( child: _buildView(), ), ); }, ); }
|
第 3 步:控制器
lib/pages/cart/buy_now/controller.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
| void onCheckout() async { List<LineItem> lineItems = [ LineItem( productId: product.id, quantity: quantity, ), ];
OrderModel res = await OrderApi.crateOrder( lineItem: lineItems, lineCoupons: lineCoupons, );
if (res.id != null) { Loading.success("Order created.");
Get.offNamed(RouteNames.cartBuyDone, arguments: res); } }
|
第 4 步:解析修正
lib/common/models/woo/order_model/line_item.dart
1 2
| factory LineItem.fromJson(Map<String, dynamic> json) => LineItem( price: double.parse("${json['price']}"),
|
第 5 步:视图
lib/pages/cart/buy_now/view.dart
1 2 3 4 5 6 7 8 9 10 11
| Widget _buildButtons() { ...
ButtonWidget.primary( LocaleKeys.placeOrderBtnPlaceOrder.tr, onTap: controller.onCheckout, ).height(50.w).expanded(),
...
|
提交代码到 git