Add size to cart

This commit is contained in:
Moe Poi ~ 2023-11-18 16:43:31 +07:00
parent e5ddccb370
commit 98877e016e
5 changed files with 97 additions and 68 deletions

View file

@ -2,21 +2,23 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class CartBox extends StatefulWidget {
const CartBox({
Key? key,
required this.controller,
required this.imageUrl,
required this.title,
required this.quantity,
required this.plus,
required this.minus,
required this.remove
}) : super(key: key);
const CartBox(
{Key? key,
required this.controller,
required this.imageUrl,
required this.title,
required this.quantity,
required this.size,
required this.plus,
required this.minus,
required this.remove})
: super(key: key);
final bool controller;
final String imageUrl;
final String title;
final int quantity;
final List<dynamic> size;
final Function() plus;
final Function() minus;
final Function() remove;
@ -26,10 +28,8 @@ class CartBox extends StatefulWidget {
}
class _CartBoxState extends State<CartBox> {
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8.0),
child: Card(
@ -43,8 +43,7 @@ class _CartBoxState extends State<CartBox> {
flex: 2,
child: CachedNetworkImage(
imageUrl: widget.imageUrl,
placeholder: (context, url) =>
const CircularProgressIndicator(
placeholder: (context, url) => const CircularProgressIndicator(
color: Color(0xff8B0000),
),
errorWidget: (context, url, error) =>
@ -64,62 +63,78 @@ class _CartBoxState extends State<CartBox> {
child: Column(
children: [
Container(
margin: const EdgeInsets.only(left:10.0, right: 10.0),
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
child: Text(
widget.title,
style: const TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.w600),
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.w600),
textAlign: TextAlign.start,
),
),
const SizedBox(height: 10,),
widget.controller == true ? Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: (){
widget.minus();
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(const Color(0xff8B0000)),
),
child: const Text("-")
),
Text(widget.quantity.toString(),
style: const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w400)
),
ElevatedButton(
onPressed: (){
widget.plus();
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(const Color(0xff8B0000)),
),
child: const Text("+")
)
],
)
: Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Quantity : ${widget.quantity.toString()}",
style: const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w400
),
),
],
),
const SizedBox(
height: 10,
),
widget.controller == true
? Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
widget.minus();
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
const Color(0xff8B0000)),
),
child: const Text("-")),
Text(widget.quantity.toString(),
style: const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w400)),
ElevatedButton(
onPressed: () {
widget.plus();
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
const Color(0xff8B0000)),
),
child: const Text("+"))
],
)
: Container(
margin:
const EdgeInsets.only(left: 10.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"Quantity : ${widget.quantity.toString()}",
style: const TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w400),
),
],
),
),
Container(
margin: const EdgeInsets.all(5.0),
child: Text(
"Size : ${widget.size.join(', ')}",
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w300,
color: Colors.white,
),
textAlign: TextAlign.left,
),
)
],
),
),
@ -128,4 +143,4 @@ class _CartBoxState extends State<CartBox> {
),
);
}
}
}

View file

@ -72,6 +72,7 @@ class _CheckoutItemsState extends State<CheckoutItems> {
"https://nekoya.moe.team/img/${productData[0]['IMAGE']}",
title: productData[0]['TITLE'],
quantity: data[index]["quantity"],
size: data[index]["size"],
plus: () {},
minus: () {},
remove: () {});
@ -84,6 +85,7 @@ class _CheckoutItemsState extends State<CheckoutItems> {
'https://i.ibb.co/QJFLZC4/La-Darknesss-Portrait.webp',
title: 'Loading...',
quantity: 0,
size: const [],
plus: () {},
minus: () {},
remove: () {},

View file

@ -218,7 +218,7 @@ Widget productDetail(context, id) {
color: Colors.white),
child: ElevatedButton.icon(
onPressed: () {
addToCart(id);
addToCart(id, selectedSize);
final dynamic cartToolTip_ =
cartToolTipKey_.currentState;
cartToolTip_.ensureTooltipVisible();

View file

@ -1,14 +1,21 @@
import 'dart:convert';
import 'package:hive/hive.dart';
Future<void> addToCart(productId) async {
Future<void> addToCart(productId, size) async {
final box = Hive.box();
var cart = jsonDecode(box.get('cart', defaultValue: '[]'));
var filteredCart = cart.where((x) => x["product_id"] == productId).toList();
var selectedSize = [];
if (filteredCart.length == 0) {
cart.add({"product_id": productId, "quantity": 1});
selectedSize.add(size);
cart.add({"product_id": productId, "quantity": 1, "size": selectedSize});
} else {
selectedSize = filteredCart[0]["size"];
selectedSize.add(size);
filteredCart[0]["size"] = selectedSize;
filteredCart[0]["quantity"]++;
cart = cart.where((x) => x["product_id"] != productId).toList();
cart.add(filteredCart[0]);
@ -29,6 +36,7 @@ Future<void> removeFromCart(productId, bool batch) async {
if (filteredCart[0]["quantity"] == 0) {
cart = cart.where((x) => x["product_id"] != productId).toList();
} else {
filteredCart[0]["size"].removeLast();
cart = cart.where((x) => x["product_id"] != productId).toList();
cart.add(filteredCart[0]);
}

View file

@ -66,8 +66,11 @@ class _CartState extends State<Cart> {
"https://nekoya.moe.team/img/${productData[0]['IMAGE']}",
title: productData[0]['TITLE'],
quantity: data[index]["quantity"],
size: data[index]["size"],
plus: () {
addToCart(data[index]["product_id"]);
var size = data[index]["size"];
addToCart(data[index]["product_id"],
size[size.length - 1]);
setState(() {
_viewCart = viewCart();
getTotal_ = getTotal();
@ -98,6 +101,7 @@ class _CartState extends State<Cart> {
'https://i.ibb.co/QJFLZC4/La-Darknesss-Portrait.webp',
title: 'Loading...',
quantity: 0,
size: const [],
plus: () {},
minus: () {},
remove: () {},