добавление изображения в lab3

This commit is contained in:
allllen4a 2024-10-14 11:45:20 +03:00
parent 5cfe4a6ad9
commit fc033c77e9

View File

@ -23,8 +23,9 @@ class Recipe {
String name;
Cuisine cuisine;
List<String> ingredients;
String imageUrl;
Recipe({required this.name, required this.cuisine, required this.ingredients});
Recipe({required this.name, required this.cuisine, required this.ingredients, required this.imageUrl});
String getDetails() {
return 'Рецепт: $name\nКухня: ${cuisine.toString().split('.').last}\nИнгредиенты: ${ingredients.join(', ')}';
@ -66,17 +67,20 @@ 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) {
final recipe = Recipe(name: name, cuisine: _selectedCuisine!, ingredients: ingredients);
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(() {});
});
@ -114,6 +118,10 @@ class _RecipeHomeState extends State<RecipeHome> {
controller: _ingredientsController,
decoration: InputDecoration(labelText: 'Ингредиенты (через запятую)'),
),
TextField(
controller: _imageUrlController,
decoration: InputDecoration(labelText: 'URL изображения'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _addRecipe,
@ -126,7 +134,10 @@ class _RecipeHomeState extends State<RecipeHome> {
itemBuilder: (context, index) {
final recipe = _recipeManager.getRecipeAt(index);
return ListTile(
title: Text(recipe?.name ?? 'Имеется ошибка'),
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() ?? ''),
);
},
@ -137,5 +148,4 @@ class _RecipeHomeState extends State<RecipeHome> {
),
);
}
}