diff --git a/lib/Item.dart b/lib/Item.dart new file mode 100644 index 0000000..c5d7a01 --- /dev/null +++ b/lib/Item.dart @@ -0,0 +1,14 @@ + + +enum ItemCategory{ + Food, + Clothing, + Electronic +} + +class Item { + final String name; + final ItemCategory category; + + Item(this.name, this.category); +} \ No newline at end of file diff --git a/lib/ShoppingCart.dart b/lib/ShoppingCart.dart new file mode 100644 index 0000000..ae83cd3 --- /dev/null +++ b/lib/ShoppingCart.dart @@ -0,0 +1,27 @@ +import 'Item.dart'; + +class ShoppingCart{ + List items = []; + + void addItem(Item item){ + items.add(item); + } + + void removeItem(Item item){ + items.remove(item); + } + + void clearItems(){ + items.clear(); + } + + List filterItems(ItemCategory category){ + return items.where((item) => item.category == category).toList(); + } +} + +Future addItemWithDelay(ShoppingCart cart, Item item) async { + await Future.delayed(Duration(seconds: 2)); + cart.addItem(item); +} + diff --git a/lib/main.dart b/lib/main.dart index 02ee5ff..d821169 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 createState() => _MyHomePageState(); + _ShoppingListScreenState createState() => _ShoppingListScreenState(); } -class _MyHomePageState extends State { - int _counter = 0; +class _ShoppingListScreenState extends State { + final ShoppingCart cart = ShoppingCart(); - void _incrementCounter() { - setState(() { - _counter++; - }); - } + List 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: [ - 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), - ), ); + ); } -} +} \ No newline at end of file diff --git a/test/widget_test.dart b/test/widget_test.dart index 125400e..0125bc0 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -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);