Adding items to cart

This commit is contained in:
2024-05-02 17:02:43 +03:00
parent 727c04d368
commit ff29598ec5
8 changed files with 354 additions and 43 deletions

View File

@@ -1,28 +1,105 @@
import 'package:flutter/material.dart';
import 'package:gymlink_module_web/components/app_bar.dart';
import 'package:gymlink_module_web/components/heading.dart';
import 'package:gymlink_module_web/tools/prefs.dart';
//TODO: Сделать получение инфы через объект
class DetailPage extends StatelessWidget {
class DetailPage extends StatefulWidget {
final String name;
final String description;
final String price;
final String id;
final Image image;
const DetailPage(
{super.key,
required this.name,
required this.description,
required this.price,
required this.id,
required this.image});
const DetailPage({
super.key,
required this.name,
required this.description,
required this.price,
required this.id,
required this.image,
});
@override
State<DetailPage> createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
bool isInCart = false;
int quantity = 0;
@override
void initState() {
super.initState();
getCart().then((value) {
debugPrint(value.toString());
setState(() {
isInCart = value.any((element) => element['id'] == widget.id);
if (isInCart) {
quantity = value
.firstWhere((element) => element['id'] == widget.id)['count'];
}
});
});
}
Widget _buildButton() {
if (!isInCart) {
return ElevatedButton(
onPressed: () async {
await addItemToCart(widget.id);
setState(() {
isInCart = true;
quantity = 1;
});
},
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(50)),
),
foregroundColor: Colors.white,
padding: const EdgeInsetsDirectional.fromSTEB(34, 10, 34, 10)),
child: const Text('Добавить в корзину'),
);
} else {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.remove),
onPressed: () async {
await removeItemFromCart(widget.id);
setState(() {
if (quantity > 1) {
quantity--;
} else {
isInCart = false;
quantity = 0;
}
});
},
),
Text('$quantity'),
IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
await addItemToCart(widget.id);
setState(() {
quantity++;
});
},
),
],
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const GymLinkAppBar(),
body: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
GymLinkHeader(title: '$name - $id'),
GymLinkHeader(title: '${widget.name} - ${widget.id}'),
Expanded(
child: SingleChildScrollView(
child: Padding(
@@ -33,7 +110,7 @@ class DetailPage extends StatelessWidget {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
image,
widget.image,
Padding(
padding:
const EdgeInsetsDirectional.fromSTEB(0, 30, 60, 60),
@@ -50,7 +127,7 @@ class DetailPage extends StatelessWidget {
padding: const EdgeInsetsDirectional.fromSTEB(
20, 15, 10, 15),
child: Text(
description,
widget.description,
style: Theme.of(context).textTheme.bodyMedium,
),
),
@@ -66,24 +143,10 @@ class DetailPage extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Стоимость $price',
'Стоимость ${widget.price}',
style: Theme.of(context).textTheme.bodyLarge,
),
ElevatedButton(
onPressed: () => {},
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context).primaryColor,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
),
foregroundColor: Colors.white,
padding: const EdgeInsetsDirectional.fromSTEB(
34, 10, 34, 10)),
child: const Text('Добавить в корзину'),
)
_buildButton()
],
),
),