Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
5cfe4a6ad9 | |||
84e1f6325f |
140
lib/main.dart
140
lib/main.dart
@ -1,70 +1,140 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Flutter Demo',
|
title: 'Рецепты',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
scaffoldBackgroundColor: Colors.purple[100],
|
||||||
useMaterial3: true,
|
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Жирнова Алена Евгениевна'),
|
home: RecipeHome(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
enum Cuisine { Italian, Indian, Chinese, Georgian }
|
||||||
const MyHomePage({super.key, required this.title});
|
|
||||||
|
|
||||||
final String title;
|
class Recipe {
|
||||||
|
String name;
|
||||||
|
Cuisine cuisine;
|
||||||
|
List<String> ingredients;
|
||||||
|
|
||||||
@override
|
Recipe({required this.name, required this.cuisine, required this.ingredients});
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
|
||||||
|
String getDetails() {
|
||||||
|
return 'Рецепт: $name\nКухня: ${cuisine.toString().split('.').last}\nИнгредиенты: ${ingredients.join(', ')}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class RecipeManager<T extends Recipe> {
|
||||||
int _counter = 0;
|
List<T> _recipes = [];
|
||||||
|
|
||||||
void _incrementCounter() {
|
Future<void> addRecipe(T recipe) async {
|
||||||
setState(() {
|
await Future.delayed(Duration(seconds: 1));
|
||||||
_counter++;
|
_recipes.add(recipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<T> getAllRecipes() => _recipes;
|
||||||
|
|
||||||
|
T? getRecipeAt(int index) {
|
||||||
|
if (index < _recipes.length) {
|
||||||
|
return _recipes[index];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RecipeListExtension<T extends Recipe> on List<T> {
|
||||||
|
void printAllRecipes() {
|
||||||
|
for (var recipe in this) {
|
||||||
|
print(recipe.getDetails());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(title: Text('Создание Рецептов')),
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
body: Padding(
|
||||||
title: Text(widget.title),
|
padding: const EdgeInsets.all(16.0),
|
||||||
),
|
|
||||||
body: Center(
|
|
||||||
|
|
||||||
child: Column(
|
child: Column(
|
||||||
|
children: [
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
TextField(
|
||||||
children: <Widget>[
|
controller: _nameController,
|
||||||
const Text(
|
decoration: InputDecoration(labelText: 'Название рецепта'),
|
||||||
'You have pushed the button this many times:',
|
),
|
||||||
|
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() ?? ''),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
Text(
|
|
||||||
'$_counter',
|
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
|
||||||
onPressed: _incrementCounter,
|
|
||||||
tooltip: 'Increment',
|
|
||||||
child: const Icon(Icons.add),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user