From 263fe00407273f24d97d198400c0cc5388472c26 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Wed, 4 Oct 2023 16:35:17 +0200 Subject: [PATCH] changed up the way that ingredients can be added --- lib/example_data.dart | 27 +++++++ lib/models/ingredient.dart | 6 +- lib/models/unit.dart | 2 - lib/pages/create_recipe_page.dart | 12 ++- lib/widgets/ingredients_bottomsheet.dart | 94 ++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 lib/example_data.dart create mode 100644 lib/widgets/ingredients_bottomsheet.dart diff --git a/lib/example_data.dart b/lib/example_data.dart new file mode 100644 index 0000000..31f956e --- /dev/null +++ b/lib/example_data.dart @@ -0,0 +1,27 @@ +import 'package:rezepte/models/unit.dart'; + +import 'models/ingredient.dart'; +import 'constants.dart' as constants; + +final List _weightAndCount = constants.units + .where((element) => + element.type == UnitType.weight || element.type == UnitType.count) + .toList(); + +final List _weight = constants.units + .where((element) => element.type == UnitType.weight) + .toList(); + +final List _count = + constants.units.where((element) => element.type == UnitType.count).toList(); + +final List _fluid = + constants.units.where((element) => element.type == UnitType.fluid).toList(); + +final List exampleIngredients = [ + Ingredient(title: 'Karotte', possibleUnits: _weightAndCount), + Ingredient(title: 'Kartoffel', possibleUnits: _weightAndCount), + Ingredient(title: 'Kaffeebohnen', possibleUnits: _weight), + Ingredient(title: 'Milch', possibleUnits: _fluid), + Ingredient(title: 'Limettenblätter', possibleUnits: _count), +]; diff --git a/lib/models/ingredient.dart b/lib/models/ingredient.dart index 27a1bfa..a16f57d 100644 --- a/lib/models/ingredient.dart +++ b/lib/models/ingredient.dart @@ -2,12 +2,12 @@ import 'unit.dart'; class Ingredient { final String title; - List possibleUnits = []; - List preferredBrands = []; + List possibleUnits; + List preferredBrands; Ingredient({ required this.title, required this.possibleUnits, - required this.preferredBrands, + this.preferredBrands = const [], }); } diff --git a/lib/models/unit.dart b/lib/models/unit.dart index 0ae76cf..eb584ab 100644 --- a/lib/models/unit.dart +++ b/lib/models/unit.dart @@ -9,8 +9,6 @@ class Unit { enum System { metric, imperial, neutral } enum UnitType { - metric, - imperial, fluid, weight, count, diff --git a/lib/pages/create_recipe_page.dart b/lib/pages/create_recipe_page.dart index 35b93b4..a1b5355 100644 --- a/lib/pages/create_recipe_page.dart +++ b/lib/pages/create_recipe_page.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:rezepte/widgets/ingredients_bottomsheet.dart'; import '../widgets/ingredients_widget.dart'; import '../models/difficulty.dart'; @@ -43,9 +44,7 @@ class _CreateRecipeState extends State { DifficultyDropdown( onChanged: _onChanged, ), - IngredientsWidget( - width: width * 0.9, - ), + FloatingActionButton(onPressed: _openIngredientBottomSheet), ], ), ), @@ -55,4 +54,11 @@ class _CreateRecipeState extends State { void _onChanged(int index) { _recipeDifficulty = Difficulty.values.elementAt(index); } + + void _openIngredientBottomSheet() { + showModalBottomSheet( + context: context, + builder: (context) => const IngredientsBottomsheet(), + ); + } } diff --git a/lib/widgets/ingredients_bottomsheet.dart b/lib/widgets/ingredients_bottomsheet.dart new file mode 100644 index 0000000..a313a31 --- /dev/null +++ b/lib/widgets/ingredients_bottomsheet.dart @@ -0,0 +1,94 @@ +import 'package:flutter/material.dart'; +import '../constants.dart' as constants; +import '../models/ingredient.dart'; +import '../models/unit.dart'; +import '../example_data.dart' as ea; + +class IngredientsBottomsheet extends StatefulWidget { + const IngredientsBottomsheet({super.key}); + + @override + State createState() => _IngredientsBottomsheetState(); +} + +class _IngredientsBottomsheetState extends State { + TextEditingController dropdownController = TextEditingController(); + final List> unitEntries = []; + final List> ingredientEntries = []; + Unit? selectedUnit; + @override + Widget build(BuildContext context) { + return BottomSheet( + onClosing: onClosing, + builder: _bottomSheetContent, + ); + } + + @override + void initState() { + super.initState(); + for (var unit in constants.units) { + unitEntries.add(DropdownMenuEntry(value: unit, label: unit.name)); + } + for (var ingredient in ea.exampleIngredients) { + ingredientEntries + .add(DropdownMenuEntry(value: ingredient, label: ingredient.title)); + } + } + + void onClosing() {} + + Widget _bottomSheetContent(BuildContext context) { + return Padding( + padding: EdgeInsets.only( + top: 20, + left: 10, + right: 10, + bottom: MediaQuery.of(context).viewInsets.bottom, + ), + child: Column( + children: [ + Row( + children: [ + Expanded( + flex: 4, + child: DropdownMenu( + dropdownMenuEntries: ingredientEntries, + enableSearch: true, + enableFilter: true, + requestFocusOnTap: true, + ), + ), + ], + ), + Row( + children: [ + Expanded( + child: TextField( + maxLines: 1, + onTapOutside: (_) => + FocusManager.instance.primaryFocus?.unfocus(), + keyboardType: TextInputType.number, + textInputAction: TextInputAction.next, + decoration: InputDecoration( + label: Text('Amount'), + ), + ), + ), + DropdownMenu( + label: const Text('Unit'), + width: 150, + requestFocusOnTap: true, + controller: dropdownController, + dropdownMenuEntries: unitEntries, + onSelected: (unit) => setState(() { + selectedUnit = unit; + }), + ) + ], + ) + ], + ), + ); + } +}