app/lib/screens/cart.dart

164 lines
5.7 KiB
Dart
Raw Normal View History

2022-04-30 04:49:09 +02:00
import 'package:intl/intl.dart';
2022-04-26 11:13:57 +02:00
import 'package:flutter/material.dart';
import 'package:nekoya_flutter/api/api.dart';
import 'package:nekoya_flutter/components/cart_box.dart';
2022-04-30 17:20:42 +02:00
import 'package:nekoya_flutter/components/cart_button.dart';
import 'package:nekoya_flutter/components/menu.dart';
2022-04-26 11:13:57 +02:00
import 'package:nekoya_flutter/data/cart.dart';
2022-04-30 04:49:09 +02:00
import 'package:nekoya_flutter/screens/checkout.dart';
2022-04-26 11:13:57 +02:00
class Cart extends StatefulWidget {
const Cart({Key? key}) : super(key: key);
@override
State<Cart> createState() => _CartState();
}
class _CartState extends State<Cart> {
2022-04-27 14:08:00 +02:00
Future<dynamic> _viewCart = viewCart();
2022-04-26 11:13:57 +02:00
@override
Widget build(BuildContext context) {
2022-04-30 04:49:09 +02:00
Future<dynamic> getTotal() async {
dynamic totalPrice = 0;
var orderData = await viewCart();
await orderData.forEach((x) async {
var product = await getProduct(x['product_id']);
totalPrice += product[0]['PRICE'] * x['quantity'];
});
2022-04-30 04:51:08 +02:00
return Future.delayed(const Duration(seconds: 2), () {
2022-04-30 04:49:09 +02:00
return totalPrice;
});
}
2022-05-12 05:28:55 +02:00
Future<dynamic> getTotal_ = getTotal();
2022-04-26 11:13:57 +02:00
return Scaffold(
backgroundColor: const Color(0xff1b1c1e),
appBar: AppBar(
title: const Text('Cart'),
centerTitle: true,
backgroundColor: const Color(0xff212226),
),
2022-04-30 04:49:09 +02:00
body: Container(
margin: const EdgeInsets.only(bottom: 80.0),
child: FutureBuilder<dynamic>(
future: _viewCart,
builder: (context, snapshot) {
if (snapshot.hasData) {
var data = snapshot.data;
2022-04-30 17:20:42 +02:00
if (data.isNotEmpty) {
return ListView.builder(
itemCount: data!.length,
itemBuilder: (context, index) {
return FutureBuilder<dynamic>(
future: getProduct(data[index]["product_id"]),
builder: (context, snapshotx) {
if (snapshotx.hasData) {
var productData = snapshotx.data;
if (productData != null) {
return CartBox(
controller: true,
2022-05-12 05:28:55 +02:00
imageUrl: "https://nekoya.moe.team/img/${productData[0]['IMAGE']}",
2022-04-30 17:20:42 +02:00
title: productData[0]['TITLE'],
quantity: data[index]["quantity"],
plus: () {
addToCart(data[index]["product_id"]);
setState(() {
_viewCart = viewCart();
2022-05-12 05:28:55 +02:00
getTotal_ = getTotal();
2022-04-30 17:20:42 +02:00
});
},
minus: () {
removeFromCart(data[index]["product_id"], false);
setState(() {
_viewCart = viewCart();
2022-05-12 05:28:55 +02:00
getTotal_ = getTotal();
2022-04-30 17:20:42 +02:00
});
},
remove: () {
removeFromCart(data[index]["product_id"], true);
setState(() {
_viewCart = viewCart();
2022-05-12 05:28:55 +02:00
getTotal_ = getTotal();
2022-04-30 17:20:42 +02:00
});
}
);
}
2022-04-30 04:49:09 +02:00
}
2022-04-30 17:20:42 +02:00
return CartBox(
controller: true,
imageUrl: 'https://i.ibb.co/QJFLZC4/La-Darknesss-Portrait.webp',
title: 'Loading...',
quantity: 0,
plus: () {},
minus: () {},
remove: () {},
);
}
);
},
);
} else {
return Padding(
padding: const EdgeInsets.only(bottom: 200.0),
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/cart-empty.webp')
)
),
),
);
}
2022-04-30 04:49:09 +02:00
}
2022-04-26 11:13:57 +02:00
2022-04-30 04:49:09 +02:00
return const Center(
child: CircularProgressIndicator(
color: Color(0xff8B0000),
),
2022-04-26 11:13:57 +02:00
);
}
2022-04-30 04:49:09 +02:00
),
),
2022-04-30 17:20:42 +02:00
floatingActionButton: FutureBuilder<dynamic>(
2022-05-12 05:28:55 +02:00
future: getTotal_,
2022-04-30 17:20:42 +02:00
builder: (context, snapshot) {
if (snapshot.hasData) {
var price = snapshot.data;
if (price != 0) {
return CartButton(
icon: const Icon(Icons.shopping_cart_checkout),
text1: "Rp ${NumberFormat('#,##0.00', 'ID').format(price)}",
text2: "Checkout",
refer: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const Checkout())
);
},
);
}
return CartButton(
icon: const Icon(Icons.shopping_cart_outlined),
text1: "Cart is Empty",
text2: "Shop Now",
refer: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const Menu(initialScreen: 2))
);
},
2022-04-30 04:49:09 +02:00
);
2022-04-30 17:20:42 +02:00
}
2022-04-26 11:13:57 +02:00
2022-04-30 17:20:42 +02:00
return const SizedBox(width: 1.0,);
}
2022-04-30 04:49:09 +02:00
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
2022-04-26 11:13:57 +02:00
);
}
}