From 84e1f6325fcf86b126cf54db8d4ce403b424783c Mon Sep 17 00:00:00 2001 From: allllen4a Date: Thu, 19 Sep 2024 15:11:28 +0300 Subject: [PATCH] lab2 --- lib/main.dart | 145 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 107 insertions(+), 38 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index ea3d0ee..5f06a4b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,71 +1,140 @@ import 'package:flutter/material.dart'; void main() { - runApp(const MyApp()); + runApp(MyApp()); } class MyApp extends StatelessWidget { - const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'Рецепты', theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), - useMaterial3: true, + scaffoldBackgroundColor: Colors.purple[100], ), - home: const MyHomePage(title: 'Жирнова Алена Евгениевна'), + home: RecipeHome(), ); } } -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); +enum Cuisine { Italian, Indian, Chinese, Georgian } - final String title; +class Recipe { + String name; + Cuisine cuisine; + List ingredients; - @override - State createState() => _MyHomePageState(); + Recipe({required this.name, required this.cuisine, required this.ingredients}); + + String getDetails() { + return 'Рецепт: $name\nКухня: ${cuisine.toString().split('.').last}\nИнгредиенты: ${ingredients.join(', ')}'; + } } -class _MyHomePageState extends State { - int _counter = 0; +class RecipeManager { + List _recipes = []; - void _incrementCounter() { - setState(() { - _counter++; - }); + Future addRecipe(T recipe) async { + await Future.delayed(Duration(seconds: 1)); // Имитация асинхронной операции + _recipes.add(recipe); + } + + List getAllRecipes() => _recipes; + + T? getRecipeAt(int index) { + if (index < _recipes.length) { + return _recipes[index]; + } + return null; // Вернём null, если индекс вне диапазона + } +} + +extension RecipeListExtension on List { + void printAllRecipes() { + for (var recipe in this) { + print(recipe.getDetails()); + } + } +} + +class RecipeHome extends StatefulWidget { + @override + _RecipeHomeState createState() => _RecipeHomeState(); +} + +class _RecipeHomeState extends State { + final RecipeManager _recipeManager = RecipeManager(); + final TextEditingController _nameController = TextEditingController(); + final TextEditingController _ingredientsController = TextEditingController(); + Cuisine? _selectedCuisine; + + void _addRecipe() { + final name = _nameController.text; + final ingredients = _ingredientsController.text.split(',').map((s) => s.trim()).toList(); + + if (name.isNotEmpty && ingredients.isNotEmpty && _selectedCuisine != null) { + final recipe = Recipe(name: name, cuisine: _selectedCuisine!, ingredients: ingredients); + _recipeManager.addRecipe(recipe).then((_) { + _nameController.clear(); + _ingredientsController.clear(); + _selectedCuisine = null; + setState(() {}); // Обновляем интерфейс + }); + } } @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text(widget.title), - ), - body: Center( - + appBar: AppBar(title: Text('Создание Рецептов')), + body: Padding( + padding: const EdgeInsets.all(16.0), child: Column( - - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', + children: [ + TextField( + controller: _nameController, + decoration: InputDecoration(labelText: 'Название рецепта'), ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, + DropdownButton( + hint: Text('Выберите кухню'), + value: _selectedCuisine, + onChanged: (Cuisine? newValue) { + setState(() { + _selectedCuisine = newValue; + }); + }, + items: Cuisine.values.map((Cuisine cuisine) { + return DropdownMenuItem( + value: cuisine, + child: Text(cuisine.toString().split('.').last), + ); + }).toList(), + ), + TextField( + controller: _ingredientsController, + decoration: InputDecoration(labelText: 'Ингредиенты (через запятую)'), + ), + SizedBox(height: 20), + ElevatedButton( + onPressed: _addRecipe, + child: Text('Добавить Рецепт'), + ), + SizedBox(height: 20), + Expanded( + child: ListView.builder( + itemCount: _recipeManager.getAllRecipes().length, + itemBuilder: (context, index) { + final recipe = _recipeManager.getRecipeAt(index); + return ListTile( + title: Text(recipe?.name ?? 'Имеется ошибка'), + subtitle: Text(recipe?.getDetails() ?? ''), + ); + }, + ), ), ], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), ); } - -} +} \ No newline at end of file