From 3faad5c4baec32a0fb143e3671f91d97f12e42cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9A=D1=80?= =?UTF-8?q?=D1=8E=D0=BA=D0=BE=D0=B2?= Date: Sun, 13 Oct 2024 21:38:57 +0400 Subject: [PATCH] =?UTF-8?q?lab=5F4=5Fdone=20=D0=BD=D0=BE=20=D1=8D=D1=82?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B5=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20,=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B4=D0=BE=20=D0=B3=D0=BB=D1=8F=D0=BD=D1=83=D1=82?= =?UTF-8?q?=D1=8C=20=D1=87=D1=83=D1=82=D1=8C=20=D0=B5=D1=89=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/{ => domain/models}/Item.dart | 0 lib/main.dart | 126 +---------------- lib/presentation/pages/details_page.dart | 29 ++++ lib/presentation/pages/home_page.dart | 140 +++++++++++++++++++ lib/{ShoppingCart.dart => shoppingCart.dart} | 2 +- 5 files changed, 175 insertions(+), 122 deletions(-) rename lib/{ => domain/models}/Item.dart (100%) create mode 100644 lib/presentation/pages/details_page.dart create mode 100644 lib/presentation/pages/home_page.dart rename lib/{ShoppingCart.dart => shoppingCart.dart} (91%) diff --git a/lib/Item.dart b/lib/domain/models/Item.dart similarity index 100% rename from lib/Item.dart rename to lib/domain/models/Item.dart diff --git a/lib/main.dart b/lib/main.dart index 93ea795..d8c2185 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,5 @@ -import 'dart:math'; import 'package:flutter/material.dart'; - -import 'Item.dart'; -import 'ShoppingCart.dart'; +import 'presentation/pages/home_page.dart'; void main() { runApp(const MyApp()); @@ -13,123 +10,10 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( - home: ShoppingListScreen(), - ); - } -} - -class ShoppingListScreen extends StatefulWidget { - const ShoppingListScreen({super.key}); - - @override - _ShoppingListScreenState createState() => _ShoppingListScreenState(); -} - -class _ShoppingListScreenState extends State { - final ShoppingCart cart = ShoppingCart(); - - List categories = [ - ItemCategory.food, - ItemCategory.clothing, - ItemCategory.electronic - ]; - final Random random = Random(); - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Список покупок'), - ), - body: Column( - children: [ - ElevatedButton( - onPressed: () async { - ItemCategory category = - categories[random.nextInt(categories.length)]; - String itemName; - String imageUrl; - - switch (category) { - case ItemCategory.food: - itemName = 'Food${random.nextInt(100)}'; - imageUrl = - 'static/imgDatasets/food_for_cart/Image_${random.nextInt(5)}.jpg'; - break; - case ItemCategory.clothing: - itemName = 'Clothing${random.nextInt(100)}'; - imageUrl = - 'static/imgDatasets/clothing_for_cart/clothing_${random.nextInt(5)}.jpg'; - break; - case ItemCategory.electronic: - itemName = 'Electronic${random.nextInt(100)}'; - imageUrl = - 'static/imgDatasets/electronic_for_cart/electronic_${random.nextInt(5)}.jpg'; - break; - } - - await addItemWithDelay(cart, Item(itemName, category, imageUrl)); - setState(() {}); - }, - child: const Text('Добавить товар'), - ), - ElevatedButton( - onPressed: () { - cart.clearItems(); - setState(() {}); - }, - child: const Text('Очистить корзину'), - ), - Expanded( - child: ListView.builder( - itemCount: cart.items.length, - itemBuilder: (context, index) { - return Card( - margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 10), - child: Row( - children: [ - ClipRRect( - borderRadius: BorderRadius.circular(10.0), - child: Container( - width: 100, - height: 100, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(10.0), - image: DecorationImage( - image: AssetImage(cart.items[index].imgUrl), - fit: BoxFit.cover, - ), - ), - ), - ), - const SizedBox(width: 10), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - cart.items[index].name, - style: const TextStyle( - fontSize: 16, - fontWeight: FontWeight.bold, - ), - ), - Text( - cart.items[index].category.toString().split('.').last, - style: const TextStyle(color: Colors.grey), - ), - ], - ), - ), - ], - ), - ); - }, - ), - ), - ], - ), + return MaterialApp( + title: 'Shopping App', + debugShowCheckedModeBanner: false, + home: const HomePage(), ); } } diff --git a/lib/presentation/pages/details_page.dart b/lib/presentation/pages/details_page.dart new file mode 100644 index 0000000..cb4e388 --- /dev/null +++ b/lib/presentation/pages/details_page.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import '../../domain/models/Item.dart'; + +class DetailsPage extends StatelessWidget { + final Item item; + + const DetailsPage({super.key, required this.item}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(item.name), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Image.asset(item.imgUrl), // Показываем картинку товара + const SizedBox(height: 10), + Text('Категория: ${item.category.toString().split('.').last}'), + const SizedBox(height: 10), + Text(item.name), + ], + ), + ), + ); + } +} diff --git a/lib/presentation/pages/home_page.dart b/lib/presentation/pages/home_page.dart new file mode 100644 index 0000000..c61e4c4 --- /dev/null +++ b/lib/presentation/pages/home_page.dart @@ -0,0 +1,140 @@ +import 'package:flutter/material.dart'; +import '../../domain/models/Item.dart'; +import '../../shoppingCart.dart'; +import 'details_page.dart'; +import 'dart:math'; + +class HomePage extends StatefulWidget { + const HomePage({super.key}); + + @override + _HomePageState createState() => _HomePageState(); +} + +class _HomePageState extends State { + final ShoppingCart cart = ShoppingCart(); + final Random random = Random(); + + List categories = [ + ItemCategory.food, + ItemCategory.clothing, + ItemCategory.electronic + ]; + + // Хранение состояния лайков + List likedItems = []; + + // Метод для генерации случайного товара + Item generateRandomItem() { + ItemCategory category = categories[random.nextInt(categories.length)]; + String itemName; + String imageUrl; + + switch (category) { + case ItemCategory.food: + itemName = 'Food${random.nextInt(100)}'; + imageUrl = + 'static/imgDatasets/food_for_cart/Image_${random.nextInt(5)}.jpg'; + break; + case ItemCategory.clothing: + itemName = 'Clothing${random.nextInt(100)}'; + imageUrl = + 'static/imgDatasets/clothing_for_cart/clothing_${random.nextInt(5)}.jpg'; + break; + case ItemCategory.electronic: + itemName = 'Electronic${random.nextInt(100)}'; + imageUrl = + 'static/imgDatasets/electronic_for_cart/electronic_${random.nextInt(5)}.jpg'; + break; + } + + return Item(itemName, category, imageUrl); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Список покупок'), + actions: [ + IconButton( + icon: const Icon(Icons.clear_all), + onPressed: () { + cart.clearItems(); + likedItems.clear(); + setState(() {}); + }, + ), + ], + ), + body: Column( + children: [ + ElevatedButton( + onPressed: () { + setState(() { + cart.addItem(generateRandomItem()); + likedItems.add(false); + }); + }, + child: const Text('Добавить товар'), + ), + Expanded( + child: ListView.builder( + itemCount: cart.items.length, + itemBuilder: (context, index) { + return Card( + margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 10), + elevation: 4, + child: ListTile( + contentPadding: const EdgeInsets.all(16.0), + leading: ClipRRect( + borderRadius: BorderRadius.circular(10.0), + child: Image.asset( + cart.items[index].imgUrl, + width: 80, + height: 80, + fit: BoxFit.cover, + ), + ), + title: Text( + cart.items[index].name, + style: const TextStyle(fontSize: 18), + ), + subtitle: Text( + cart.items[index].category.toString().split('.').last, + style: const TextStyle(color: Colors.grey), + ), + trailing: IconButton( + icon: Icon( + likedItems[index] ? Icons.thumb_up : Icons.thumb_up_off_alt, + color: likedItems[index] ? Colors.red : Colors.grey, + ), + onPressed: () { + setState(() { + likedItems[index] = !likedItems[index]; + }); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(likedItems[index] ? 'Товар лайкнут!' : 'Лайк убран!'), + ), + ); + }, + ), + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => DetailsPage(item: cart.items[index]), + ), + ); + }, + ), + ); + }, + ), + ), + ], + ), + ); + } +} diff --git a/lib/ShoppingCart.dart b/lib/shoppingCart.dart similarity index 91% rename from lib/ShoppingCart.dart rename to lib/shoppingCart.dart index 3f0509c..86f7230 100644 --- a/lib/ShoppingCart.dart +++ b/lib/shoppingCart.dart @@ -1,4 +1,4 @@ -import 'Item.dart' show Item, ItemCategory; +import 'domain/models/Item.dart'; class ShoppingCart { List items = [];