Skip to content

Commit

Permalink
cart
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindhIppili committed Jul 21, 2021
1 parent 058e63c commit ec0f300
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 67 deletions.
12 changes: 11 additions & 1 deletion lib/screens/components/cart_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ class CartCounter extends StatefulWidget {
class _CartCounterState extends State<CartCounter> {
int id;
_CartCounterState({required this.id});
String count = "";
int numofItems = 0;
@override
Widget build(BuildContext context) {
final prov = Provider.of<AddToCart>(context, listen: false);
setState(() {
count = prov.getCount(this.id).toString().padLeft(2, "0");
});
return Consumer<AddToCart>(
builder: (context, item, child) => SizedBox(
child: child,
Expand All @@ -30,11 +34,14 @@ class _CartCounterState extends State<CartCounter> {
children: [
buildButton(Icons.remove, () {
prov.updateCart(this.id, "remove");
setState(() {
count = prov.getCount(this.id).toString().padLeft(2, "0");
});
}),
Padding(
padding: EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2),
child: Text(
prov.getCount(this.id).toString().padLeft(2, "0"),
count,
style: Theme.of(context).textTheme.headline6,
),
),
Expand All @@ -49,6 +56,9 @@ class _CartCounterState extends State<CartCounter> {
product: products
.firstWhere((element) => element.id == this.id)));
}
setState(() {
count = prov.getCount(this.id).toString().padLeft(2, "0");
});
})
],
),
Expand Down
156 changes: 90 additions & 66 deletions lib/screens/view_cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,84 +34,108 @@ class _ViewCartState extends State<ViewCart> {
builder: (context, cartItems, child) => Container(
child: child,
),
child: ListView.builder(
itemCount: cartItems.length,
itemBuilder: (context, index) => Card(
elevation: 3,
child: Row(
children: [
Expanded(
flex: 4,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
constraints:
BoxConstraints(maxHeight: 200, maxWidth: 200),
padding: EdgeInsets.all(kDefaultPaddin / 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: cartItems[index].product.color,
),
child: Image.asset(
cartItems[index].product.image,
child: Stack(
children: [
ListView.builder(
itemCount: cartItems.length,
itemBuilder: (context, index) => Card(
elevation: 3,
child: Row(
children: [
Expanded(
flex: 4,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
constraints:
BoxConstraints(maxHeight: 200, maxWidth: 200),
padding: EdgeInsets.all(kDefaultPaddin / 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: cartItems[index].product.color,
),
child: Image.asset(
cartItems[index].product.image,
),
),
),
),
),
),
SizedBox(
width: 25,
),
Expanded(
flex: 6,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
cartItems[index].product.title,
style: Theme.of(context)
.textTheme
.headline5!
.copyWith(color: Colors.black),
),
Text(
"\$${cartItems[index].product.price.toString()}\n",
style: TextStyle(fontSize: 20, color: Colors.black),
),
Row(
SizedBox(
width: 25,
),
Expanded(
flex: 6,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
buildButton(Icons.remove, () {
if (cartItems[index].count > 0)
setState(() {
cartItems[index].count--;
});
}),
Padding(
padding: EdgeInsets.symmetric(
horizontal: kDefaultPaddin / 2),
child: Text(
cartItems[index].count.toString().padLeft(2, "0"),
style: Theme.of(context).textTheme.headline6,
),
Text(
cartItems[index].product.title,
style: Theme.of(context)
.textTheme
.headline5!
.copyWith(color: Colors.black),
),
Text(
"\$${cartItems[index].product.price.toString()}\n",
style: TextStyle(fontSize: 20, color: Colors.black),
),
buildButton(Icons.add, () {
setState(() {
cartItems[index].count++;
});
})
Row(
children: [
buildButton(Icons.remove, () {
if (cartItems[index].count > 0)
setState(() {
cartItems[index].count--;
});
}),
Padding(
padding: EdgeInsets.symmetric(
horizontal: kDefaultPaddin / 2),
child: Text(
cartItems[index]
.count
.toString()
.padLeft(2, "0"),
style: Theme.of(context).textTheme.headline6,
),
),
buildButton(Icons.add, () {
setState(() {
cartItems[index].count++;
});
})
],
)
],
)
],
),
),
),
],
),
],
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
decoration: BoxDecoration(color: Colors.white),
child: Text("Total: ${getTotal()}",
style: TextStyle(
fontSize: 30,
))),
)
],
),
),
);
}

int getTotal() {
int c = 0;
for (var i = 0; i < cartItems.length; i++) {
c += cartItems[i].product.price * cartItems[i].count;
}
return c;
}

SizedBox buildButton(IconData icon, Function() press) {
return SizedBox(
height: 32,
Expand Down

0 comments on commit ec0f300

Please sign in to comment.