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/tools/prefs.dart'; List> cart = [ { "name": "Протеин", "image": "product.png", "price": "120", "details": "Test details", "id": "34fa3126-bfaf-5dec-8f4a-b246c097ef73" }, { "name": "Протеин", "image": "product.png", "price": "150", "details": "Test details", "id": "34a26e82-7656-5e98-a44a-c2d01d0b1ad1123" }, { "name": "Протеин", "image": "product.png", "price": "250", "details": "Test details", "id": "4fb204b7-3f9e-52a2-bed1-415c00a31a37123" }, { "name": "Протеин", "image": "product.png", "price": "300", "details": "Test details", "id": "09b2f5bb-683e-5c39-ae89-b8e152fa8bcf123" }, { "name": "Протеин", "image": "product.png", "price": "100", "details": "Test details", "id": "cd1b6817-db94-5394-be1d-af88af79749f123" } ]; //TODO: Сделать правильное удаление из корзины по одному class BasketPage extends StatefulWidget { const BasketPage({super.key}); @override State createState() => _BasketPageState(); } class _BasketPageState extends State { List> cartItems = []; int totalPrice = 0; @override void initState() { super.initState(); getCart().then((value) { setState(() { cartItems = cart .where((element) => value.any((e) => e['id'] == element['id'])) .toList(); totalPrice = cartItems.fold(0, (sum, item) => sum + int.parse(item['price'])); }); }); } void removeItem(String id) async { setState(() { cartItems.removeWhere((element) => element['id'] == id); totalPrice = cartItems.fold(0, (sum, item) => sum + int.parse(item['price'])); }); await removeItemFromCart(id); } Future _deleteItemAlert(String id, String name) async { return showDialog( context: context, barrierDismissible: true, builder: (BuildContext context) { return AlertDialog( title: const Text('Удаление из корзины'), content: SingleChildScrollView( child: ListBody( children: [ Text('Вы действительно хотите убрать "$name" из корзины?'), ], ), ), actions: [ TextButton( child: const Text('Отмена'), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( child: const Text('Удалить'), onPressed: () { removeItem(id); Navigator.of(context).pop(); }, ), ], ); }, ); } Future _clearCartAlert() async { return showDialog( context: context, barrierDismissible: true, builder: (BuildContext context) { return AlertDialog( title: const Text('Очистка корзины'), content: const SingleChildScrollView( child: ListBody( children: [ Text('Вы действительно хотите очистить корзину?'), ], ), ), actions: [ TextButton( child: const Text('Отмена'), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( child: const Text('Очистить'), onPressed: () { clearCart(); setState(() { cartItems = []; }); Navigator.of(context).pop(); }, ), ], ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: const GymLinkAppBar(), 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), 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: Row( children: [ Expanded( 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.asset( item['image'], width: 50, ), onTap: () => _deleteItemAlert(item['id'], item['name']), ); }, ), ), const Spacer(), Padding( padding: const EdgeInsetsDirectional.symmetric( horizontal: 10, vertical: 10), child: Column( children: [ Text( 'Итого: $totalPrice', ), ElevatedButton( onPressed: () {}, 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), ], ), ), ], ), ); } }