PIbd-31_Zhirnova_A_E_PMD/lib/main.dart

141 lines
4.0 KiB
Dart
Raw Permalink Normal View History

2024-09-09 21:40:52 +04:00
import 'package:flutter/material.dart';
void main() {
2024-09-19 16:11:28 +04:00
runApp(MyApp());
2024-09-09 21:40:52 +04:00
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
2024-09-19 16:11:28 +04:00
title: 'Рецепты',
2024-09-09 21:40:52 +04:00
theme: ThemeData(
2024-09-19 16:11:28 +04:00
scaffoldBackgroundColor: Colors.purple[100],
2024-09-09 21:40:52 +04:00
),
2024-09-19 16:11:28 +04:00
home: RecipeHome(),
2024-09-09 21:40:52 +04:00
);
}
}
2024-09-19 16:11:28 +04:00
enum Cuisine { Italian, Indian, Chinese, Georgian }
2024-09-09 21:40:52 +04:00
2024-09-19 16:11:28 +04:00
class Recipe {
String name;
Cuisine cuisine;
List<String> ingredients;
2024-09-09 21:40:52 +04:00
2024-09-19 16:11:28 +04:00
Recipe({required this.name, required this.cuisine, required this.ingredients});
String getDetails() {
return 'Рецепт: $name\nКухня: ${cuisine.toString().split('.').last}\nИнгредиенты: ${ingredients.join(', ')}';
}
}
class RecipeManager<T extends Recipe> {
List<T> _recipes = [];
Future<void> addRecipe(T recipe) async {
2024-09-19 16:13:14 +04:00
await Future.delayed(Duration(seconds: 1));
2024-09-19 16:11:28 +04:00
_recipes.add(recipe);
}
List<T> getAllRecipes() => _recipes;
T? getRecipeAt(int index) {
if (index < _recipes.length) {
return _recipes[index];
}
2024-09-19 16:13:14 +04:00
return null;
2024-09-19 16:11:28 +04:00
}
}
extension RecipeListExtension<T extends Recipe> on List<T> {
void printAllRecipes() {
for (var recipe in this) {
print(recipe.getDetails());
}
}
}
class RecipeHome extends StatefulWidget {
2024-09-09 21:40:52 +04:00
@override
2024-09-19 16:11:28 +04:00
_RecipeHomeState createState() => _RecipeHomeState();
2024-09-09 21:40:52 +04:00
}
2024-09-19 16:11:28 +04:00
class _RecipeHomeState extends State<RecipeHome> {
final RecipeManager<Recipe> _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();
2024-09-09 21:40:52 +04:00
2024-09-19 16:11:28 +04:00
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;
2024-09-19 16:13:14 +04:00
setState(() {});
2024-09-19 16:11:28 +04:00
});
}
2024-09-09 21:40:52 +04:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-09-19 16:11:28 +04:00
appBar: AppBar(title: Text('Создание Рецептов')),
body: Padding(
padding: const EdgeInsets.all(16.0),
2024-09-09 21:40:52 +04:00
child: Column(
2024-09-19 16:11:28 +04:00
children: [
TextField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Название рецепта'),
2024-09-09 21:40:52 +04:00
),
2024-09-19 16:11:28 +04:00
DropdownButton<Cuisine>(
hint: Text('Выберите кухню'),
value: _selectedCuisine,
onChanged: (Cuisine? newValue) {
setState(() {
_selectedCuisine = newValue;
});
},
items: Cuisine.values.map((Cuisine cuisine) {
return DropdownMenuItem<Cuisine>(
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() ?? ''),
);
},
),
2024-09-09 21:40:52 +04:00
),
],
),
),
);
}
2024-09-19 16:13:14 +04:00
2024-09-19 16:11:28 +04:00
}