lab_2_done

This commit is contained in:
Алексей Крюков 2024-09-17 14:19:51 +04:00
parent d7813dde56
commit 57991a6f2e
4 changed files with 110 additions and 46 deletions

14
lib/Item.dart Normal file
View File

@ -0,0 +1,14 @@
enum ItemCategory{
Food,
Clothing,
Electronic
}
class Item {
final String name;
final ItemCategory category;
Item(this.name, this.category);
}

27
lib/ShoppingCart.dart Normal file
View File

@ -0,0 +1,27 @@
import 'Item.dart';
class ShoppingCart{
List<Item> items = [];
void addItem(Item item){
items.add(item);
}
void removeItem(Item item){
items.remove(item);
}
void clearItems(){
items.clear();
}
List<Item> filterItems(ItemCategory category){
return items.where((item) => item.category == category).toList();
}
}
Future<void> addItemWithDelay(ShoppingCart cart, Item item) async {
await Future.delayed(Duration(seconds: 2));
cart.addItem(item);
}

View File

@ -1,70 +1,93 @@
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'Item.dart';
import 'ShoppingCart.dart';
void main() {
runApp(const MyApp());
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Крюков Алексей Игоревич || ПИбд-31 '),
home: ShoppingListScreen(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
class ShoppingListScreen extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
_ShoppingListScreenState createState() => _ShoppingListScreenState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
class _ShoppingListScreenState extends State<ShoppingListScreen> {
final ShoppingCart cart = ShoppingCart();
void _incrementCounter() {
setState(() {
_counter++;
});
}
List<ItemCategory> categories = [ItemCategory.Food, ItemCategory.Clothing, ItemCategory.Electronic];
final Random random = Random();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
title: Text('Список покупок'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
body: Column(
children: [
// Кнопка для добавления товара
ElevatedButton(
onPressed: () async {
// Определяем текущую категорию
ItemCategory category = categories[random.nextInt(categories.length)];
String itemName;
switch (category) {
case ItemCategory.Food:
itemName = 'Food${random.nextInt(100)}';
break;
case ItemCategory.Clothing:
itemName = 'Clothing${random.nextInt(100)}';
break;
case ItemCategory.Electronic:
itemName = 'Electronic${random.nextInt(100)}';
break;
}
// Добавление товара с задержкой
await addItemWithDelay(cart, Item(itemName, category));
setState(() {;
});
},
child: Text('Добавить товар'),
),
// Кнопка для очистки корзины
ElevatedButton(
onPressed: () {
cart.clearItems();
setState(() {});
},
child: Text('Очистить корзину'),
),
Expanded(
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(cart.items[index].name),
subtitle: Text(cart.items[index].category.toString().split('.').last),
);
},
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), );
);
}
}
}

View File

@ -13,7 +13,7 @@ import 'package:flutter_test_app/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);