Skip to content

Commit

Permalink
task: cart checkout to order
Browse files Browse the repository at this point in the history
  • Loading branch information
Atuoha committed Oct 11, 2022
1 parent 9578893 commit 8a67327
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 77 deletions.
2 changes: 1 addition & 1 deletion lib/models/order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'cart.dart';

class Order {
final String id;
final int totalPrice;
final double totalPrice;
final List<CartItem> items;

Order({
Expand Down
7 changes: 6 additions & 1 deletion lib/providers/cart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import 'package:multivendor_shop/models/cart.dart';
class CartData extends ChangeNotifier {
var totalPrice = 0.0;

void clearCart() {
_cartItems.clear();
notifyListeners();
}

bool isItemOnCart(String prodId) {
return _cartItems.any((item) => item.prodId == prodId);
}
Expand Down Expand Up @@ -61,7 +66,7 @@ class CartData extends ChangeNotifier {
return totalPrice;
}

get cartItems {
List<CartItem> get cartItems {
return [..._cartItems];
}

Expand Down
4 changes: 3 additions & 1 deletion lib/providers/order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class OrderData extends ChangeNotifier {

_orderItems.add(item);
notifyListeners();

print('ADDED TO ORDER');
}

void removeFromOrder(String id) {
Expand All @@ -27,7 +29,7 @@ class OrderData extends ChangeNotifier {
return _orderItems.length;
}

get orderItems {
List<Order> get orderItems {
return [..._orderItems];
}

Expand Down
3 changes: 2 additions & 1 deletion lib/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:multivendor_shop/views/main/seller/dashboard_screens/orders.dart
import 'package:multivendor_shop/views/main/seller/dashboard_screens/statistics.dart';
import 'package:multivendor_shop/views/main/seller/dashboard_screens/store_setup.dart';
import 'package:multivendor_shop/views/main/seller/seller_bottomNav.dart';

import '../views/auth/account_type_selector.dart';
import '../views/main/customer/customer_bottomNav.dart';
import '../views/main/customer/order.dart';
import '../views/main/seller/dashboard_screens/upload_product.dart';
import '../views/splash/entry.dart';
import '../views/splash/splash.dart';
Expand All @@ -27,4 +27,5 @@ var routes = {
StoreSetupScreen.routeName: (context) => const StoreSetupScreen(),
StatisticsScreen.routeName: (context) => const StatisticsScreen(),
AccountBalanceScreen.routeName: (context) => const AccountBalanceScreen(),
CustomerOrderScreen.routeName: (context)=>const CustomerOrderScreen(),
};
157 changes: 84 additions & 73 deletions lib/views/main/customer/cart.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:flutter/material.dart';
import 'package:multivendor_shop/views/main/customer/order.dart';
import 'package:provider/provider.dart';
import '../../../constants/colors.dart';
import '../../../models/cart.dart';
import '../../../models/order.dart';
import '../../../providers/cart.dart';
import '../../../providers/order.dart';
import '../../../utilities/build_cart.dart';

class CartScreen extends StatefulWidget {
Expand All @@ -14,89 +18,96 @@ class CartScreen extends StatefulWidget {
enum Operation { checkoutCart, clearCart }

class _CartScreenState extends State<CartScreen> {
// confirmation for checkout and clear cart
confirmOptions(Operation operation) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Icon(
operation == Operation.clearCart
? Icons.remove_shopping_cart_outlined
: Icons.shopping_cart_checkout_outlined,
color: primaryColor,
),
Text(
operation == Operation.clearCart
? 'Confirm Clear'
: 'Confirm Checkout',
style: const TextStyle(
@override
Widget build(BuildContext context) {
var cartData = Provider.of<CartData>(context, listen: false);
var orderData = Provider.of<OrderData>(context, listen: false);

_clearCart() {
// clearing cart
cartData.clearCart();
}

_checkOut() {
// checking out cart items
List<CartItem> items = cartData.cartItems;
var totalPrice = cartData.cartTotalPrice;
orderData.addToOrder(Order(id: '', totalPrice: totalPrice, items: items));
_clearCart();
Navigator.of(context).pushNamed(CustomerOrderScreen.routeName);
}

// confirmation for checkout and clear cart
confirmOptions(Operation operation) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Icon(
operation == Operation.clearCart
? Icons.remove_shopping_cart_outlined
: Icons.shopping_cart_checkout_outlined,
color: primaryColor,
fontWeight: FontWeight.w600,
),
Text(
operation == Operation.clearCart
? 'Confirm Clear'
: 'Confirm Checkout',
style: const TextStyle(
color: primaryColor,
fontWeight: FontWeight.w600,
),
),
],
),
content: Text(
operation == Operation.clearCart
? 'Are you sure you want to clear cart?'
: 'Are you sure you want to checkout cart?',
style: const TextStyle(
color: primaryColor,
),
],
),
content: Text(
operation == Operation.clearCart
? 'Are you sure you want to clear cart?'
: 'Are you sure you want to checkout cart?',
style: const TextStyle(
color: primaryColor,
),
),
actions: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
actions: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
onPressed: () =>
operation == Operation.clearCart ? _clearCart() : _checkOut(),
child: const Text(
'Yes',
style: TextStyle(
color: Colors.white,
onPressed: () {
operation == Operation.clearCart ? _clearCart() : _checkOut();
Navigator.of(context).pop();
},
child: const Text(
'Yes',
style: TextStyle(
color: Colors.white,
),
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
onPressed: () => Navigator.of(context).pop(),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.white,
onPressed: () => Navigator.of(context).pop(),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.white,
),
),
),
),
],
),
);
}

_clearCart() {
//TODO: Implement clear cart
}

_checkOut() {
//TODO: Implement checkout cart
}

var quantity = 1;

@override
Widget build(BuildContext context) {
var cartData = Provider.of<CartData>(context, listen: false);
],
),
);
}

// remove from cart
void removeFromCart(String prodId) {
Expand Down
88 changes: 88 additions & 0 deletions lib/views/main/customer/order.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../constants/colors.dart';
import 'package:provider/provider.dart';
import '../../../constants/colors.dart';
import '../../../providers/order.dart';

class CustomerOrderScreen extends StatelessWidget {
static const routeName = '/customer_orders';

const CustomerOrderScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
var orderData = Provider.of<OrderData>(context, listen: false);

SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
systemNavigationBarColor: litePrimary,
statusBarIconBrightness: Brightness.dark,
systemNavigationBarDividerColor: Colors.grey,
statusBarBrightness: Brightness.dark,
),
);
return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: const Text(
'Orders',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
body: Text(''),
bottomSheet: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
const Text(
'Total:',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 22,
),
),
const SizedBox(width: 5),
Text(
'\$${orderData.orderTotalPrice}',
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 28,
color: primaryColor,
),
),
],
),
Directionality(
textDirection: TextDirection.rtl,
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
icon: const Icon(
Icons.shopping_cart_checkout,
color: Colors.white,
),
onPressed: () => {},
label: const Text(
'Checkout',
style: TextStyle(
color: Colors.white,
),
),
),
)
],
),
);
}
}

0 comments on commit 8a67327

Please sign in to comment.