Skip to content

Commit

Permalink
task: refactoring cart into buildCart
Browse files Browse the repository at this point in the history
  • Loading branch information
Atuoha committed Oct 11, 2022
1 parent fb12c7e commit 9578893
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 136 deletions.
132 changes: 132 additions & 0 deletions lib/utilities/build_cart.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
import '../../../constants/colors.dart';

Dismissible buildCart(
void Function(String prodId) removeFromCart,
item,
BuildContext context,
void Function(String id) increaseQuantity,
void Function(String id) reduceQuantity,
) {
return Dismissible(
onDismissed: (direction) => removeFromCart(item.prodId),
direction: DismissDirection.endToStart,
background: Container(
height: 115,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.red,
),
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 20),
child: const Icon(
Icons.delete_forever,
color: Colors.white,
size: 40,
),
),
confirmDismiss: (direction) => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Remove ${item.prodName}'),
content: Text(
'Are you sure you want to remove ${item.prodName} from cart?',
),
actions: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
onPressed: () => Navigator.of(context).pop(true),
child: const Text(
'Yes',
style: TextStyle(
color: Colors.white,
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
onPressed: () => Navigator.of(context).pop(false),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.white,
),
),
),
],
),
),
key: ValueKey(item.id),
child: Card(
elevation: 3,
child: ListTile(
contentPadding: const EdgeInsets.only(
left: 10,
right: 10,
top: 5,
),
leading: CircleAvatar(
backgroundColor: primaryColor,
backgroundImage: NetworkImage(item.prodImgUrl),
),
title: Text(
item.prodName,
style: const TextStyle(
fontSize: 16,
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('\$${item.totalPrice}'),
const SizedBox(height: 5),
Row(
children: [
GestureDetector(
onTap: () => increaseQuantity(item.id),
child: const Icon(
Icons.add,
color: primaryColor,
),
),
const SizedBox(width: 10),
Text(
item.quantity.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
const SizedBox(width: 10),
GestureDetector(
onTap: () => reduceQuantity(item.id),
child: const Icon(
Icons.remove,
color: primaryColor,
),
),
],
),
],
),
trailing: IconButton(
onPressed: () => removeFromCart(item.prodId),
icon: const Icon(
Icons.delete_forever,
color: primaryColor,
),
),
),
),
);
}
143 changes: 7 additions & 136 deletions lib/views/main/customer/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../constants/colors.dart';
import '../../../providers/cart.dart';
import '../../../utilities/build_cart.dart';

class CartScreen extends StatefulWidget {
const CartScreen({Key? key}) : super(key: key);
Expand Down Expand Up @@ -173,142 +174,12 @@ class _CartScreenState extends State<CartScreen> {
itemCount: data.cartItemCount,
itemBuilder: (context, index) {
var item = data.cartItems[index];
return Dismissible(
onDismissed: (direction) =>
removeFromCart(item.prodId),
direction: DismissDirection.endToStart,
background: Container(
height: 115,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(10),
color: Colors.red,
),
alignment: Alignment.centerRight,
padding:
const EdgeInsets.only(right: 20),
child: const Icon(
Icons.delete_forever,
color: Colors.white,
size: 40,
),
),
confirmDismiss: (direction) => showDialog(
context: context,
builder: (context) => AlertDialog(
title:
Text('Remove ${item.prodName}'),
content: Text(
'Are you sure you want to remove ${item.prodName} from cart?'),
actions: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10),
),
),
onPressed: () =>
Navigator.of(context)
.pop(true),
child: const Text(
'Yes',
style: TextStyle(
color: Colors.white,
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10),
),
),
onPressed: () =>
Navigator.of(context)
.pop(false),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.white,
),
),
),
],
),
),
key: ValueKey(item.id),
child: Card(
elevation: 3,
child: ListTile(
contentPadding: const EdgeInsets.only(
left: 10,
right: 10,
top: 5,
),
leading: CircleAvatar(
backgroundColor: primaryColor,
backgroundImage:
NetworkImage(item.prodImgUrl),
),
title: Text(
item.prodName,
style: const TextStyle(
fontSize: 16,
),
),
subtitle: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text('\$${item.totalPrice}'),
const SizedBox(height: 5),
Row(
children: [
GestureDetector(
onTap: () =>
increaseQuantity(
item.id),
child: const Icon(
Icons.add,
color: primaryColor,
),
),
const SizedBox(width: 10),
Text(
item.quantity.toString(),
style: const TextStyle(
fontWeight:
FontWeight.bold,
fontSize: 18,
),
),
const SizedBox(width: 10),
GestureDetector(
onTap: () =>
reduceQuantity(item.id),
child: const Icon(
Icons.remove,
color: primaryColor,
),
),
],
),
],
),
trailing: IconButton(
onPressed: () =>
removeFromCart(item.prodId),
icon: const Icon(
Icons.delete_forever,
color: primaryColor,
),
),
),
),
return buildCart(
removeFromCart,
item,
context,
increaseQuantity,
reduceQuantity,
);
},
),
Expand Down

0 comments on commit 9578893

Please sign in to comment.