Add: getting products from API
This commit is contained in:
@@ -2,9 +2,10 @@ import 'package:flutter/material.dart';
|
||||
import 'package:gymlink_module_web/components/app_bar.dart';
|
||||
import 'package:gymlink_module_web/components/basket_item_card.dart';
|
||||
import 'package:gymlink_module_web/components/heading.dart';
|
||||
import 'package:gymlink_module_web/interfaces/items.dart';
|
||||
import 'package:gymlink_module_web/pages/order_confirmation.dart';
|
||||
import 'package:gymlink_module_web/providers/cart.dart';
|
||||
import 'package:gymlink_module_web/providers/main.dart';
|
||||
import 'package:gymlink_module_web/tools/items.dart';
|
||||
import 'package:gymlink_module_web/tools/prefs.dart';
|
||||
import 'package:gymlink_module_web/tools/routes.dart';
|
||||
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
||||
@@ -56,46 +57,47 @@ class BasketPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _BasketPageState extends State<BasketPage> {
|
||||
List<Map<String, dynamic>> cartItems = [];
|
||||
int totalPrice = 0;
|
||||
List<GymItem> cartItems = [];
|
||||
double totalPrice = 0;
|
||||
List<GymItem> gymCart = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
getCart().then((value) {
|
||||
setState(() {
|
||||
cartItems = value.map((element) {
|
||||
final item = cart.firstWhere((e) => e['id'] == element['id']);
|
||||
return {...item, 'count': element['count'] as int};
|
||||
}).toList();
|
||||
totalPrice = cartItems.fold(
|
||||
0,
|
||||
(sum, item) =>
|
||||
sum + int.parse(item['price']) * item['count'] as int);
|
||||
});
|
||||
});
|
||||
Future.microtask(() => getCart().then((value) async {
|
||||
final items = await getItems(context);
|
||||
setState(() {
|
||||
gymCart = items;
|
||||
cartItems = value.map((element) {
|
||||
final item = gymCart.firstWhere((e) => e.id == element['id']);
|
||||
item.localCount = element['count'] as int;
|
||||
return item;
|
||||
}).toList();
|
||||
totalPrice = cartItems.fold(
|
||||
0, (sum, item) => sum + item.price * item.localCount);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
void _updateCart() {
|
||||
Provider.of<CartProvider>(context, listen: false).updateCartLength();
|
||||
Provider.of<GymLinkProvider>(context, listen: false).onTokenReceived('123');
|
||||
}
|
||||
|
||||
void removeItem(String id) async {
|
||||
final item = cartItems.firstWhere((element) => element['id'] == id);
|
||||
final item = cartItems.firstWhere((element) => element.id == id);
|
||||
bool toDelete = false;
|
||||
setState(() {
|
||||
if (item['count'] > 1) {
|
||||
item['count']--;
|
||||
cartItems[cartItems.indexOf(item)]['count'] = item['count'];
|
||||
if (item.localCount > 1) {
|
||||
item.localCount--;
|
||||
cartItems[cartItems.indexOf(item)].localCount = item.localCount;
|
||||
} else {
|
||||
toDelete = true;
|
||||
}
|
||||
totalPrice = cartItems.fold(0,
|
||||
(sum, item) => sum + int.parse(item['price']) * item['count'] as int);
|
||||
totalPrice =
|
||||
cartItems.fold(0, (sum, item) => sum + item.price * item.localCount);
|
||||
});
|
||||
if (toDelete) {
|
||||
await _deleteItemAlert(id, item['name']);
|
||||
await _deleteItemAlert(id, item.title);
|
||||
} else {
|
||||
await removeItemFromCart(id);
|
||||
}
|
||||
@@ -103,15 +105,16 @@ class _BasketPageState extends State<BasketPage> {
|
||||
|
||||
void addItem(String id) async {
|
||||
setState(() {
|
||||
final item = cartItems.firstWhere((element) => element['id'] == id,
|
||||
orElse: () => {
|
||||
...cart.firstWhere((element) => element['id'] == id),
|
||||
'count': 0
|
||||
});
|
||||
item['count']++;
|
||||
cartItems[cartItems.indexOf(item)]['count'] = item['count'];
|
||||
totalPrice = cartItems.fold(0,
|
||||
(sum, item) => sum + int.parse(item['price']) * item['count'] as int);
|
||||
final item =
|
||||
cartItems.firstWhere((element) => element.id == id, orElse: () {
|
||||
final cartItem = gymCart.firstWhere((element) => element.id == id);
|
||||
cartItem.localCount = 0;
|
||||
return cartItem;
|
||||
});
|
||||
item.localCount++;
|
||||
cartItems[cartItems.indexOf(item)].localCount = item.localCount;
|
||||
totalPrice =
|
||||
cartItems.fold(0, (sum, item) => sum + item.price * item.localCount);
|
||||
});
|
||||
await addItemToCart(id);
|
||||
}
|
||||
@@ -142,7 +145,7 @@ class _BasketPageState extends State<BasketPage> {
|
||||
onPressed: () {
|
||||
removeItemFromCart(id);
|
||||
setState(() {
|
||||
cartItems.removeWhere((element) => element['id'] == id);
|
||||
cartItems.removeWhere((element) => element.id == id);
|
||||
});
|
||||
if (mounted) {
|
||||
_updateCart();
|
||||
@@ -224,76 +227,20 @@ class _BasketPageState extends State<BasketPage> {
|
||||
body: Column(
|
||||
children: [
|
||||
const GymLinkHeader(title: "Корзина"),
|
||||
cartItems.isEmpty
|
||||
? Expanded(
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('Корзина пуста',
|
||||
style: Theme.of(context).textTheme.bodyLarge),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius:
|
||||
BorderRadius.all(Radius.circular(50)),
|
||||
),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Вернуться назад'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
: Expanded(
|
||||
child: _buildRowOrCol(
|
||||
context: context,
|
||||
children: [
|
||||
Expanded(
|
||||
child: LazyLoadScrollView(
|
||||
onEndOfPage: _onLoad,
|
||||
child: ListView.builder(
|
||||
itemCount: cartItems.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = cartItems[index];
|
||||
return BasketItemCard(
|
||||
name: item['name'],
|
||||
price: item['price'],
|
||||
id: item['id'],
|
||||
image: Image(
|
||||
image: AssetImage('assets/${item['image']}'),
|
||||
width: 50,
|
||||
),
|
||||
onTapPlus: () => addItem(item['id'].toString()),
|
||||
onTapMinus: () {
|
||||
removeItem(item['id'].toString());
|
||||
},
|
||||
quantity: item['count'].toString(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildSpacer(),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.symmetric(
|
||||
horizontal: 10, vertical: 10),
|
||||
gymCart.isEmpty
|
||||
? const Expanded(
|
||||
child: Center(child: CircularProgressIndicator()))
|
||||
: cartItems.isEmpty
|
||||
? Expanded(
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
'Итого: $totalPrice',
|
||||
),
|
||||
Text('Корзина пуста',
|
||||
style: Theme.of(context).textTheme.bodyLarge),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
CustomPageRoute(
|
||||
builder: (context) =>
|
||||
const OrderConfirmationPage(),
|
||||
),
|
||||
),
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
shape: const RoundedRectangleBorder(
|
||||
@@ -302,27 +249,89 @@ class _BasketPageState extends State<BasketPage> {
|
||||
),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Оформить заказ'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: _clearCartAlert,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius:
|
||||
BorderRadius.all(Radius.circular(50))),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Очистить корзину'),
|
||||
child: const Text('Вернуться назад'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 50),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
: Expanded(
|
||||
child: _buildRowOrCol(
|
||||
context: context,
|
||||
children: [
|
||||
Expanded(
|
||||
child: LazyLoadScrollView(
|
||||
onEndOfPage: _onLoad,
|
||||
child: ListView.builder(
|
||||
itemCount: cartItems.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = cartItems[index];
|
||||
return BasketItemCard(
|
||||
name: item.title,
|
||||
price: item.price.toString(),
|
||||
id: item.id,
|
||||
image: Image(
|
||||
image: NetworkImage(item.images[0].url),
|
||||
width: 50,
|
||||
),
|
||||
onTapPlus: () =>
|
||||
addItem(item.id.toString()),
|
||||
onTapMinus: () {
|
||||
removeItem(item.id.toString());
|
||||
},
|
||||
quantity: item.localCount.toString(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildSpacer(),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.symmetric(
|
||||
horizontal: 10, vertical: 10),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'Итого: $totalPrice',
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
CustomPageRoute(
|
||||
builder: (context) =>
|
||||
const OrderConfirmationPage(),
|
||||
),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor:
|
||||
Theme.of(context).primaryColor,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius:
|
||||
BorderRadius.all(Radius.circular(50)),
|
||||
),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Оформить заказ'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: _clearCartAlert,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor:
|
||||
Theme.of(context).primaryColor,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(50))),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Очистить корзину'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 50),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user