lab_4_done но это не точно , надо глянуть чуть еще

This commit is contained in:
Алексей Крюков 2024-10-13 21:38:57 +04:00
parent 8626bc54ef
commit 3faad5c4ba
5 changed files with 175 additions and 122 deletions

View File

@ -1,8 +1,5 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'presentation/pages/home_page.dart';
import 'Item.dart';
import 'ShoppingCart.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
@ -13,123 +10,10 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const MaterialApp( return MaterialApp(
home: ShoppingListScreen(), title: 'Shopping App',
); debugShowCheckedModeBanner: false,
} home: const HomePage(),
}
class ShoppingListScreen extends StatefulWidget {
const ShoppingListScreen({super.key});
@override
_ShoppingListScreenState createState() => _ShoppingListScreenState();
}
class _ShoppingListScreenState extends State<ShoppingListScreen> {
final ShoppingCart cart = ShoppingCart();
List<ItemCategory> 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),
),
],
),
),
],
),
);
},
),
),
],
),
); );
} }
} }

View File

@ -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),
],
),
),
);
}
}

View File

@ -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<HomePage> {
final ShoppingCart cart = ShoppingCart();
final Random random = Random();
List<ItemCategory> categories = [
ItemCategory.food,
ItemCategory.clothing,
ItemCategory.electronic
];
// Хранение состояния лайков
List<bool> 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]),
),
);
},
),
);
},
),
),
],
),
);
}
}

View File

@ -1,4 +1,4 @@
import 'Item.dart' show Item, ItemCategory; import 'domain/models/Item.dart';
class ShoppingCart { class ShoppingCart {
List<Item> items = []; List<Item> items = [];