From 3bf70d2f9721bed85feea40946c3a78dc9dfb108 Mon Sep 17 00:00:00 2001 From: Moe <moe@chocola.dev> Date: Fri, 29 Apr 2022 20:32:29 +0700 Subject: [PATCH] Add total price to transactions page --- lib/components/transaction_box.dart | 111 ++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 24 deletions(-) diff --git a/lib/components/transaction_box.dart b/lib/components/transaction_box.dart index 72b1a92..9a1fde5 100644 --- a/lib/components/transaction_box.dart +++ b/lib/components/transaction_box.dart @@ -45,6 +45,19 @@ class _TransactionBoxState extends State<TransactionBox> { Widget build(BuildContext context) { var orderData = jsonDecode(widget.data); + Future<dynamic> getTotal() async { + dynamic totalPrice = 0; + + await orderData.forEach((x) async { + var product = await getProduct(x['product_id']); + totalPrice += product[0]['PRICE'] * x['quantity']; + }); + + return Future.delayed(const Duration(seconds: 5), (){ + return totalPrice; + }); + } + return ExpansionTile( backgroundColor: const Color(0xff212226), iconColor: Colors.white, @@ -100,35 +113,85 @@ class _TransactionBoxState extends State<TransactionBox> { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), - child: ListView.builder( - scrollDirection: Axis.vertical, - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - itemCount: orderData.length, - itemBuilder: (context, index) { - return FutureBuilder<dynamic>( - future: getProduct(orderData[index]['product_id']), - builder: (context, snapshot) { - if (snapshot.hasData) { - var productData = snapshot.data; - return TransactionProductBox( - imageUrl: 'https://nekoya.moe.team/img/' + productData[0]['IMAGE'], - title: productData[0]['TITLE'], - quantity: orderData[index]["quantity"], - ); - } + child: Column( + children: [ + ListView.builder( + scrollDirection: Axis.vertical, + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + itemCount: orderData.length, + itemBuilder: (context, index) { + return FutureBuilder<dynamic>( + future: getProduct(orderData[index]['product_id']), + builder: (context, snapshot) { + if (snapshot.hasData) { + var productData = snapshot.data; + return TransactionProductBox( + imageUrl: 'https://nekoya.moe.team/img/' + productData[0]['IMAGE'], + title: productData[0]['TITLE'], + quantity: orderData[index]["quantity"], + ); + } - return const TransactionProductBox( - imageUrl: 'https://i.ibb.co/QJFLZC4/La-Darknesss-Portrait.webp', - title: 'Loading...', - quantity: 0, + return const TransactionProductBox( + imageUrl: 'https://i.ibb.co/QJFLZC4/La-Darknesss-Portrait.webp', + title: 'Loading...', + quantity: 0, + ); + } ); } - ); - } + ), + + Container( + margin: const EdgeInsets.only(left: 15.0, right: 15.0), + child: const Divider( + color: Colors.white, + ), + ), + + Container( + margin: const EdgeInsets.only(top: 15.0, right: 15.0, bottom: 20.0), + child: FutureBuilder<dynamic>( + future: getTotal(), + builder: (context, snapshot) { + if (snapshot.hasData) { + var price = snapshot.data; + return Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text( + "Total : ${price.toString()}", + style: const TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.w600 + ), + ) + ], + ); + } + + return Row( + mainAxisAlignment: MainAxisAlignment.end, + children: const [ + Text( + "Total : -", + style: TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.w600 + ), + ) + ], + ); + } + ), + ), + ], ), ), - ) + ), ], ); }