Skip to content

Commit

Permalink
Order 목록을 서버에 저장 #14
Browse files Browse the repository at this point in the history
  • Loading branch information
korca0220 committed Mar 21, 2021
1 parent 92f93ed commit bbc3735
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
4 changes: 4 additions & 0 deletions shop_app/lib/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Uri kPrdouctUrl() {
Uri kProductUrlWithId(String id) {
return Uri.parse('');
}

Uri kOrderUrl() {
return Uri.parse('');
}
28 changes: 26 additions & 2 deletions shop_app/lib/providers/orders.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:shop_app/const.dart';
import 'package:shop_app/providers/cart.dart';
import 'package:http/http.dart' as http;

class OrderItem {
final String id;
Expand All @@ -21,11 +25,31 @@ class Orders with ChangeNotifier {
return [..._orders];
}

void addOrder(List<CartItem> cartProducts, double total) {
Future<void> addOrder(List<CartItem> cartProducts, double total) async {
final timestamp = DateTime.now();
final response = await http.post(
kOrderUrl(),
body: json.encode(
{
'amount': total,
'dateTime': timestamp.toIso8601String(),
'products': cartProducts
.map(
(cp) => {
'id': cp.id,
'title': cp.title,
'quantity': cp.quantity,
'price': cp.price
},
)
.toList()
},
),
);
_orders.insert(
0,
OrderItem(
id: DateTime.now().toString(),
id: json.decode(response.body)['name'],
amount: total,
products: cartProducts,
dateTime: DateTime.now(),
Expand Down
62 changes: 46 additions & 16 deletions shop_app/lib/screens/cart_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,7 @@ class CartScreen extends StatelessWidget {
),
backgroundColor: Theme.of(context).primaryColor,
),
TextButton(
onPressed: () {
Provider.of<Orders>(context, listen: false).addOrder(
cart.items.values.toList(),
cart.totalAmount,
);
cart.clear();
Navigator.of(context).pushNamed(OrdersScreen.routeId);
},
child: Text(
'ORDER NOW',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
),
), // )
OrderButton(cart: cart),
],
),
),
Expand All @@ -80,3 +65,48 @@ class CartScreen extends StatelessWidget {
);
}
}

class OrderButton extends StatefulWidget {
const OrderButton({
Key key,
@required this.cart,
}) : super(key: key);

final Cart cart;

@override
_OrderButtonState createState() => _OrderButtonState();
}

class _OrderButtonState extends State<OrderButton> {
bool _isLoading = false;

@override
Widget build(BuildContext context) {
return TextButton(
onPressed: (widget.cart.totalAmount <= 0 || _isLoading)
? null
: () async {
setState(() {
_isLoading = true;
});
await Provider.of<Orders>(context, listen: false).addOrder(
widget.cart.items.values.toList(),
widget.cart.totalAmount,
);
setState(() {
_isLoading = false;
});
widget.cart.clear();
},
child: _isLoading
? CircularProgressIndicator()
: Text(
'ORDER NOW',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
),
);
}
}

0 comments on commit bbc3735

Please sign in to comment.