From 7eea286c7f001c10cc3fbab36f58622754119a8c Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 11 Feb 2025 15:37:46 +0100 Subject: [PATCH] Added fields to metadata page --- .../create_recipe_pages/metadata_page.dart | 176 +++++++++++++++++- 1 file changed, 173 insertions(+), 3 deletions(-) diff --git a/lib/pages/create_recipe_pages/metadata_page.dart b/lib/pages/create_recipe_pages/metadata_page.dart index 1615414..be2e19e 100644 --- a/lib/pages/create_recipe_pages/metadata_page.dart +++ b/lib/pages/create_recipe_pages/metadata_page.dart @@ -1,12 +1,182 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; -class MetadataPage extends StatelessWidget { +import '../../services/providers/recipe_provider.dart'; +import '../../src/enums.dart'; +import '../../services/tools.dart' + show getEnumValueName, durationToFormattedString; +import '../../widgets/duration_picker.dart'; + +class MetadataPage extends StatefulWidget { const MetadataPage({super.key}); + @override + State createState() => _MetadataPageState(); +} + +class _MetadataPageState extends State { + late RecipeProvider _recipeProvider; + @override Widget build(BuildContext context) { - return const Center( - child: Text('Metadata'), + _recipeProvider = context.watch(); + return Center( + child: Column( + children: [ + TextField( + decoration: InputDecoration( + label: Text('Title'), + ), + ), + TextField( + decoration: InputDecoration( + label: Text('Description'), + ), + ), + DropdownButton( + value: _recipeProvider.recipe.difficulty, + items: Difficulty.values + .map( + (e) => DropdownMenuItem( + value: e, + child: Text(getEnumValueName(e)), + ), + ) + .toList(), + onChanged: (value) => setState(() { + _recipeProvider.updateDifficulty(value ?? Difficulty.notSelected); + }), + ), + DropdownMenu( + dropdownMenuEntries: + getDropdownMenuEntriesFromEnum(MealCategory.values), + enableFilter: true, + label: Text('Category'), + ), + DropdownMenu( + dropdownMenuEntries: getDropdownMenuEntriesFromEnum(Cuisine.values), + enableFilter: true, + label: Text('Cuisine'), + ), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text('Preparation time'), + _recipeProvider.recipe.prepTime.inMinutes == 0 + ? IconButton( + onPressed: () => showDialog( + context: context, + builder: (context) => _getDurationDialog( + context, + _recipeProvider.updatePrepTime, + _recipeProvider.recipe.prepTime), + ), + icon: Icon(Icons.add), + ) + : TextButton( + onPressed: () => showDialog( + context: context, + builder: (context) => _getDurationDialog( + context, + _recipeProvider.updatePrepTime, + _recipeProvider.recipe.prepTime), + ), + child: Text( + durationToFormattedString( + _recipeProvider.recipe.prepTime), + ), + ) + ], + ), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text('Cooking time'), + _recipeProvider.recipe.prepTime.inMinutes == 0 + ? IconButton( + onPressed: () => showDialog( + context: context, + builder: (context) => _getDurationDialog( + context, + _recipeProvider.updateCookTime, + _recipeProvider.recipe.cookTime), + ), + icon: Icon(Icons.add), + ) + : TextButton( + onPressed: () => showDialog( + context: context, + builder: (context) => _getDurationDialog( + context, + _recipeProvider.updateCookTime, + _recipeProvider.recipe.cookTime), + ), + child: Text( + durationToFormattedString( + _recipeProvider.recipe.cookTime), + ), + ) + ], + ), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text('Total Time'), + Padding(padding: EdgeInsets.only(left: 10)), + Text(durationToFormattedString(_recipeProvider.recipe.totalTime)), + ], + ), + ], + ), ); } } + +List> getDropdownMenuEntriesFromEnum( + List values) { + return values + .map( + (e) => DropdownMenuEntry( + value: e, + label: getEnumValueName(e), + ), + ) + .toList(); +} + +Dialog _getDurationDialog( + BuildContext context, + void Function(Duration duration) update, + Duration initialDuration, +) { + final width = MediaQuery.of(context).size.width * 0.8; + final height = MediaQuery.of(context).size.height * 0.6; + Duration selectedTime = initialDuration; + return Dialog( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + DurationPicker( + initialDuration: initialDuration, + onChangedCallback: (duration) => selectedTime = duration, + height: width * 1.25 > height ? height : width * 1.25, + width: width, + ), + Align( + alignment: Alignment.bottomRight, + child: Padding( + padding: const EdgeInsets.fromLTRB(0, 20, 20, 20), + child: FloatingActionButton.extended( + label: Text('Save'), + icon: Icon(Icons.save), + onPressed: () { + update(selectedTime); + Navigator.of(context).pop(); + }, + ), + ), + ), + ], + ), + ); +}