changed difficulty logic

This commit is contained in:
SomnusVeritas
2023-10-03 23:06:20 +02:00
parent 10463bb61a
commit 518631f37f
4 changed files with 24 additions and 11 deletions

View File

@@ -1,8 +1,8 @@
class Difficulty { class DifficultyUtil {
static List<String> get difficulties { static List<String> get difficulties {
return List<String>.generate(_Difficulty.values.length, return List<String>.generate(Difficulty.values.length,
(index) => _Difficulty.values.elementAt(index).name); (index) => Difficulty.values.elementAt(index).name);
} }
} }
enum _Difficulty { veryEasy, easy, intermediate, hard, veryHard } enum Difficulty { veryEasy, easy, intermediate, hard, veryHard }

View File

@@ -5,7 +5,7 @@ import 'steps.dart';
class Recipe { class Recipe {
final String title; final String title;
final String description; final String description;
final Difficulty? difficulty; final DifficultyUtil? difficulty;
final List<Ingredient> ingredients = []; final List<Ingredient> ingredients = [];
final Steps steps = Steps(); final Steps steps = Steps();

View File

@@ -40,7 +40,9 @@ class _CreateRecipeState extends State<CreateRecipe> {
label: Text('Description'), label: Text('Description'),
), ),
), ),
const DifficultyDropdown(), DifficultyDropdown(
onChanged: _onChanged,
),
IngredientsWidget( IngredientsWidget(
width: width * 0.9, width: width * 0.9,
), ),
@@ -49,4 +51,8 @@ class _CreateRecipeState extends State<CreateRecipe> {
), ),
); );
} }
void _onChanged(int index) {
_recipeDifficulty = Difficulty.values.elementAt(index);
}
} }

View File

@@ -2,19 +2,26 @@ import 'package:flutter/material.dart';
import '../models/difficulty.dart'; import '../models/difficulty.dart';
typedef Intcallback = void Function(int);
class DifficultyDropdown extends StatelessWidget { class DifficultyDropdown extends StatelessWidget {
const DifficultyDropdown({super.key}); const DifficultyDropdown({super.key, this.onChanged});
final Intcallback? onChanged;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
List<DropdownMenuEntry> dropdownMenuEntryList = Difficulty.difficulties List<DropdownMenuEntry> dropdownMenuEntryList = DifficultyUtil.difficulties
.map( .map((e) =>
(e) => _toDropdownMenuEntry(Difficulty.difficulties.indexOf(e), e), _toDropdownMenuEntry(DifficultyUtil.difficulties.indexOf(e), e))
)
.toList(); .toList();
return DropdownMenu( return DropdownMenu(
dropdownMenuEntries: dropdownMenuEntryList, dropdownMenuEntries: dropdownMenuEntryList,
onSelected: (value) {
if (onChanged != null) {
onChanged!(value as int);
}
},
); );
} }