Skip to content

Commit

Permalink
task: cart dismissable swipe delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
Atuoha committed Oct 11, 2022
1 parent 43daacf commit fb12c7e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 68 deletions.
Binary file added cart_remove.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions lib/models/order.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import 'cart.dart';

class Order {
final String id;
final String prodId;
final String prodName;
final String prodImg;
final int quantity;
final int price;
final int totalPrice;
final List<CartItem> items;

Order({
required this.id,
required this.prodId,
required this.prodName,
required this.prodImg,
required this.quantity,
required this.price,
required this.totalPrice,
required this.items,
});
}
8 changes: 3 additions & 5 deletions lib/providers/order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ class OrderData extends ChangeNotifier {
void addToOrder(Order order) {
Order item = Order(
id: DateTime.now().toString(),
prodId: order.prodId,
prodName: order.prodName,
price: order.price,
quantity: order.quantity,
prodImg: order.prodImg,
totalPrice: order.totalPrice,
items: order.items,
);

_orderItems.add(item);
Expand All @@ -35,6 +32,7 @@ class OrderData extends ChangeNotifier {
}

get orderTotalPrice {
totalPrice = 0.0;
for (var item in _orderItems) {
totalPrice += item.totalPrice;
}
Expand Down
177 changes: 125 additions & 52 deletions lib/views/main/customer/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,66 +173,139 @@ class _CartScreenState extends State<CartScreen> {
itemCount: data.cartItemCount,
itemBuilder: (context, index) {
var item = data.cartItems[index];
return Card(
elevation: 3,
child: ListTile(
contentPadding: const EdgeInsets.only(
left: 10,
right: 10,
top: 5,
return Dismissible(
onDismissed: (direction) =>
removeFromCart(item.prodId),
direction: DismissDirection.endToStart,
background: Container(
height: 115,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(10),
color: Colors.red,
),
leading: CircleAvatar(
backgroundColor: primaryColor,
backgroundImage:
NetworkImage(item.prodImgUrl),
alignment: Alignment.centerRight,
padding:
const EdgeInsets.only(right: 20),
child: const Icon(
Icons.delete_forever,
color: Colors.white,
size: 40,
),
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,
),
),
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),
),
const SizedBox(width: 10),
Text(
item.quantity.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
onPressed: () =>
Navigator.of(context)
.pop(true),
child: const Text(
'Yes',
style: TextStyle(
color: Colors.white,
),
const SizedBox(width: 10),
GestureDetector(
onTap: ()=>reduceQuantity(item.id),
child: const Icon(
Icons.remove,
color: primaryColor,
),
),
),
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,
),
),
),
],
),
trailing: IconButton(
onPressed: () =>
removeFromCart(item.prodId),
icon: const Icon(
Icons.delete_forever,
color: primaryColor,
),
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,
),
),
),
),
Expand Down
1 change: 0 additions & 1 deletion lib/views/main/product/sub_category_products.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../../constants/colors.dart';
import '../../../utilities/products_stream_builder.dart';

Expand Down

0 comments on commit fb12c7e

Please sign in to comment.