добавление изображения в 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; String name;
Cuisine cuisine; Cuisine cuisine;
List<String> ingredients; 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() { String getDetails() {
return 'Рецепт: $name\nКухня: ${cuisine.toString().split('.').last}\nИнгредиенты: ${ingredients.join(', ')}'; 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 RecipeManager<Recipe> _recipeManager = RecipeManager();
final TextEditingController _nameController = TextEditingController(); final TextEditingController _nameController = TextEditingController();
final TextEditingController _ingredientsController = TextEditingController(); final TextEditingController _ingredientsController = TextEditingController();
final TextEditingController _imageUrlController = TextEditingController();
Cuisine? _selectedCuisine; Cuisine? _selectedCuisine;
void _addRecipe() { void _addRecipe() {
final name = _nameController.text; final name = _nameController.text;
final ingredients = _ingredientsController.text.split(',').map((s) => s.trim()).toList(); final ingredients = _ingredientsController.text.split(',').map((s) => s.trim()).toList();
final imageUrl = _imageUrlController.text;
if (name.isNotEmpty && ingredients.isNotEmpty && _selectedCuisine != null) { if (name.isNotEmpty && ingredients.isNotEmpty && _selectedCuisine != null && imageUrl.isNotEmpty) {
final recipe = Recipe(name: name, cuisine: _selectedCuisine!, ingredients: ingredients); final recipe = Recipe(name: name, cuisine: _selectedCuisine!, ingredients: ingredients, imageUrl: imageUrl);
_recipeManager.addRecipe(recipe).then((_) { _recipeManager.addRecipe(recipe).then((_) {
_nameController.clear(); _nameController.clear();
_ingredientsController.clear(); _ingredientsController.clear();
_imageUrlController.clear();
_selectedCuisine = null; _selectedCuisine = null;
setState(() {}); setState(() {});
}); });
@ -114,6 +118,10 @@ class _RecipeHomeState extends State<RecipeHome> {
controller: _ingredientsController, controller: _ingredientsController,
decoration: InputDecoration(labelText: 'Ингредиенты (через запятую)'), decoration: InputDecoration(labelText: 'Ингредиенты (через запятую)'),
), ),
TextField(
controller: _imageUrlController,
decoration: InputDecoration(labelText: 'URL изображения'),
),
SizedBox(height: 20), SizedBox(height: 20),
ElevatedButton( ElevatedButton(
onPressed: _addRecipe, onPressed: _addRecipe,
@ -126,7 +134,10 @@ class _RecipeHomeState extends State<RecipeHome> {
itemBuilder: (context, index) { itemBuilder: (context, index) {
final recipe = _recipeManager.getRecipeAt(index); final recipe = _recipeManager.getRecipeAt(index);
return ListTile( 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() ?? ''), subtitle: Text(recipe?.getDetails() ?? ''),
); );
}, },
@ -137,5 +148,4 @@ class _RecipeHomeState extends State<RecipeHome> {
), ),
); );
} }
} }