164 lines
5.1 KiB
Dart
164 lines
5.1 KiB
Dart
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 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,
|
|
});
|
|
|
|
@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: '${widget.name} - ${widget.id}'),
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(20),
|
|
child: SizedBox(
|
|
width: MediaQuery.sizeOf(context).width,
|
|
height: MediaQuery.sizeOf(context).height,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
widget.image,
|
|
Padding(
|
|
padding:
|
|
const EdgeInsetsDirectional.fromSTEB(0, 30, 60, 60),
|
|
child: SizedBox(
|
|
width: 340,
|
|
height: MediaQuery.sizeOf(context).height,
|
|
child: Card(
|
|
elevation: 4,
|
|
color: const Color(0xFFF2F3F9),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsetsDirectional.fromSTEB(
|
|
20, 15, 10, 15),
|
|
child: Text(
|
|
widget.description,
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Align(
|
|
alignment: const AlignmentDirectional(0, -1),
|
|
child: Padding(
|
|
padding:
|
|
const EdgeInsetsDirectional.fromSTEB(0, 60, 0, 0),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
'Стоимость ${widget.price}',
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
),
|
|
_buildButton()
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
}
|