208 lines
6.1 KiB
Dart
208 lines
6.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
runApp(MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Рецепты',
|
|
theme: ThemeData(
|
|
scaffoldBackgroundColor: Colors.purple[100],
|
|
),
|
|
home: RecipeHome(),
|
|
);
|
|
}
|
|
}
|
|
|
|
enum Cuisine { Italian, Indian, Chinese, Georgian }
|
|
|
|
class Recipe {
|
|
String name;
|
|
Cuisine cuisine;
|
|
List<String> ingredients;
|
|
String imageUrl;
|
|
bool liked;
|
|
|
|
Recipe({
|
|
required this.name,
|
|
required this.cuisine,
|
|
required this.ingredients,
|
|
required this.imageUrl,
|
|
this.liked = false,
|
|
});
|
|
|
|
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 {
|
|
await Future.delayed(Duration(seconds: 1));
|
|
_recipes.add(recipe);
|
|
}
|
|
|
|
List<T> getAllRecipes() => _recipes;
|
|
|
|
T? getRecipeAt(int index) {
|
|
if (index < _recipes.length) {
|
|
return _recipes[index];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
class RecipeHome extends StatefulWidget {
|
|
@override
|
|
_RecipeHomeState createState() => _RecipeHomeState();
|
|
}
|
|
|
|
class _RecipeHomeState extends State<RecipeHome> {
|
|
final RecipeManager<Recipe> _recipeManager = RecipeManager();
|
|
final TextEditingController _nameController = TextEditingController();
|
|
final TextEditingController _ingredientsController = TextEditingController();
|
|
final TextEditingController _imageUrlController = TextEditingController();
|
|
Cuisine? _selectedCuisine;
|
|
|
|
void _addRecipe() {
|
|
final name = _nameController.text;
|
|
final ingredients = _ingredientsController.text.split(',').map((s) => s.trim()).toList();
|
|
final imageUrl = _imageUrlController.text;
|
|
|
|
if (name.isNotEmpty && ingredients.isNotEmpty && _selectedCuisine != null && imageUrl.isNotEmpty) {
|
|
final recipe = Recipe(
|
|
name: name,
|
|
cuisine: _selectedCuisine!,
|
|
ingredients: ingredients,
|
|
imageUrl: imageUrl,
|
|
);
|
|
_recipeManager.addRecipe(recipe).then((_) {
|
|
_nameController.clear();
|
|
_ingredientsController.clear();
|
|
_imageUrlController.clear();
|
|
_selectedCuisine = null;
|
|
setState(() {});
|
|
});
|
|
}
|
|
}
|
|
|
|
void _likeRecipe(int index) {
|
|
setState(() {
|
|
final recipe = _recipeManager.getRecipeAt(index);
|
|
if (recipe != null) {
|
|
recipe.liked = !recipe.liked;
|
|
final message = recipe.liked ? 'Рецепт добавлен в избранное!' : 'Лайк убран!';
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
|
|
}
|
|
});
|
|
}
|
|
|
|
void _showRecipeDetail(Recipe recipe) {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => RecipeDetail(recipe: recipe),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Создание Рецептов')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
controller: _nameController,
|
|
decoration: InputDecoration(labelText: 'Название рецепта'),
|
|
),
|
|
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: 'Ингредиенты (через запятую)'),
|
|
),
|
|
TextField(
|
|
controller: _imageUrlController,
|
|
decoration: InputDecoration(labelText: 'URL изображения'),
|
|
),
|
|
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(
|
|
leading: recipe != null && recipe.imageUrl.isNotEmpty
|
|
? Image.network(recipe.imageUrl, width: 50, height: 50, fit: BoxFit.cover)
|
|
: null,
|
|
title: Text(recipe?.name ?? 'Ошибка'),
|
|
subtitle: Text(recipe?.getDetails() ?? ''),
|
|
trailing: IconButton(
|
|
icon: Icon(recipe?.liked == true ? Icons.favorite : Icons.favorite_border),
|
|
onPressed: () => _likeRecipe(index),
|
|
),
|
|
onTap: () {
|
|
if (recipe != null) {
|
|
_showRecipeDetail(recipe);
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class RecipeDetail extends StatelessWidget {
|
|
final Recipe recipe;
|
|
|
|
RecipeDetail({required this.recipe});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(recipe.name)),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Image.network(recipe.imageUrl),
|
|
SizedBox(height: 20),
|
|
Text(recipe.getDetails(), style: TextStyle(fontSize: 18)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |