From 11266b9ab447e41e9aacfa0074eefb821804444e Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Tue, 17 Oct 2023 18:17:59 +0200 Subject: [PATCH] Refactored the code so that DifficultyDropdown is not longer needed --- lib/models/difficulty.dart | 12 ++++++++++++ lib/pages/create_recipe_page.dart | 8 +++----- lib/widgets/difficulty_dropdown.dart | 29 ---------------------------- 3 files changed, 15 insertions(+), 34 deletions(-) delete mode 100644 lib/widgets/difficulty_dropdown.dart diff --git a/lib/models/difficulty.dart b/lib/models/difficulty.dart index 1edcd3f..8465717 100644 --- a/lib/models/difficulty.dart +++ b/lib/models/difficulty.dart @@ -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> getDropdownList() { + return Difficulty.values + .map((value) => _toDropdownMenuEntry(value)) + .toList(); + } + + static DropdownMenuEntry _toDropdownMenuEntry( + Difficulty difficulty) => + DropdownMenuEntry(value: difficulty, label: getName(difficulty)); } // Only use camelCase or UpperCamelCase for names diff --git a/lib/pages/create_recipe_page.dart b/lib/pages/create_recipe_page.dart index 6d5e389..858ada1 100644 --- a/lib/pages/create_recipe_page.dart +++ b/lib/pages/create_recipe_page.dart @@ -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 { label: Text('Description'), ), ), - DifficultyDropdown( - onChanged: _onChanged, + DropdownMenu( + dropdownMenuEntries: DifficultyUtil.getDropdownList(), + onSelected: (value) {}, ), ElevatedButton( onPressed: _openIngredientBottomSheet, diff --git a/lib/widgets/difficulty_dropdown.dart b/lib/widgets/difficulty_dropdown.dart deleted file mode 100644 index 6db4686..0000000 --- a/lib/widgets/difficulty_dropdown.dart +++ /dev/null @@ -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> dropdownMenuEntryList = - Difficulty.values.map((e) => _toDropdownMenuEntry(e, e.name)).toList(); - - return DropdownMenu( - dropdownMenuEntries: dropdownMenuEntryList, - onSelected: (value) { - if (onChanged != null) { - onChanged!(value?.index ?? -1); - } - }, - ); - } - - DropdownMenuEntry _toDropdownMenuEntry( - Difficulty difficulty, String text) => - DropdownMenuEntry(value: difficulty, label: text); -}