Refactored the code so that DifficultyDropdown

is not longer needed
This commit is contained in:
SomnusVeritas
2023-10-17 18:17:59 +02:00
parent e69aab7a04
commit 11266b9ab4
3 changed files with 15 additions and 34 deletions

View File

@@ -1,3 +1,5 @@
import 'package:flutter/material.dart';
class DifficultyUtil {
/// Converts lowerCamelCase or UpperCamelCase enum-names to 'normal' Strings
static String getName(Difficulty difficulty) {
@@ -6,6 +8,16 @@ class DifficultyUtil {
name = name[0].toUpperCase() + name.substring(1);
return name;
}
static List<DropdownMenuEntry<Difficulty>> getDropdownList() {
return Difficulty.values
.map((value) => _toDropdownMenuEntry(value))
.toList();
}
static DropdownMenuEntry<Difficulty> _toDropdownMenuEntry(
Difficulty difficulty) =>
DropdownMenuEntry(value: difficulty, label: getName(difficulty));
}
// Only use camelCase or UpperCamelCase for names

View File

@@ -1,9 +1,6 @@
import 'package:flutter/material.dart';
import 'package:rezepte/widgets/ingredients_bottomsheet.dart';
import '../widgets/ingredients_widget.dart';
import '../models/difficulty.dart';
import '../widgets/difficulty_dropdown.dart';
class CreateRecipe extends StatefulWidget {
const CreateRecipe({super.key});
@@ -41,8 +38,9 @@ class _CreateRecipeState extends State<CreateRecipe> {
label: Text('Description'),
),
),
DifficultyDropdown(
onChanged: _onChanged,
DropdownMenu<Difficulty?>(
dropdownMenuEntries: DifficultyUtil.getDropdownList(),
onSelected: (value) {},
),
ElevatedButton(
onPressed: _openIngredientBottomSheet,

View File

@@ -1,29 +0,0 @@
import 'package:flutter/material.dart';
import '../models/difficulty.dart';
typedef Intcallback = void Function(int);
class DifficultyDropdown extends StatelessWidget {
const DifficultyDropdown({super.key, this.onChanged});
final Intcallback? onChanged;
@override
Widget build(BuildContext context) {
List<DropdownMenuEntry<Difficulty>> dropdownMenuEntryList =
Difficulty.values.map((e) => _toDropdownMenuEntry(e, e.name)).toList();
return DropdownMenu<Difficulty?>(
dropdownMenuEntries: dropdownMenuEntryList,
onSelected: (value) {
if (onChanged != null) {
onChanged!(value?.index ?? -1);
}
},
);
}
DropdownMenuEntry<Difficulty> _toDropdownMenuEntry(
Difficulty difficulty, String text) =>
DropdownMenuEntry(value: difficulty, label: text);
}