From 621a863f45273a148c4bd9b8bb3a5c7777f65c7d Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Mon, 26 Jul 2021 21:50:10 +0200 Subject: [PATCH] switching dropdown on add pizza event page to pizza recipe box --- lib/pages/AddPizzaEventPage.dart | 42 +++++++++++++++----------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/lib/pages/AddPizzaEventPage.dart b/lib/pages/AddPizzaEventPage.dart index e71ffc7..359ac8e 100644 --- a/lib/pages/AddPizzaEventPage.dart +++ b/lib/pages/AddPizzaEventPage.dart @@ -2,11 +2,14 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:fluttericon/font_awesome5_icons.dart'; -import 'package:hive/hive.dart'; + import 'package:pizzaplanner/entities/PizzaEvent.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; import 'package:pizzaplanner/util.dart'; +import 'package:hive/hive.dart'; +import 'package:hive_flutter/hive_flutter.dart'; + class AddPizzaEventPage extends StatefulWidget { @override AddPizzaEventPageState createState() => AddPizzaEventPageState(); @@ -16,27 +19,12 @@ class AddPizzaEventPageState extends State { String name = ""; bool initialized = false; late PizzaRecipe pizzaRecipe; - late List pizzaTypes; int pizzaCount = 1; int doughBallSize = 250; DateTime eventTime = DateTime.now(); bool nameValidation = false; - @override - void initState() { - super.initState(); - getRecipes().then((pTypes) { - this.pizzaTypes = pTypes; - this.pizzaRecipe = this.pizzaTypes.first; - setState(() {this.initialized = true;}); - }, onError: (e, stacktrace) { - print(e); - print(stacktrace); - Navigator.pop(context); - }); - } - @override Widget build(BuildContext context) { return Scaffold( @@ -75,21 +63,29 @@ class AddPizzaEventPageState extends State { children: [ Icon(FontAwesome5.pizza_slice), Container(width: 25), - Expanded( - child: this.initialized ? // Only render the dropdown if the recipes have been loaded from storage - DropdownButton( + ValueListenableBuilder( + valueListenable: Hive.box("PizzaRecipes").listenable(), + builder: (context, Box box, widget) { + if (box.isEmpty){ + return Text("Loading Pizza Recipes..."); + } + this.pizzaRecipe = box.values.first; + return Expanded( + child: DropdownButton( value: this.pizzaRecipe.name, onChanged: (String? newType) { - setState(() => this.pizzaRecipe = this.pizzaTypes.firstWhere((pizzaRecipe) => pizzaRecipe.name == newType)); + setState(() => this.pizzaRecipe = box.values.firstWhere((pizzaRecipe) => pizzaRecipe.name == newType)); }, - items: this.pizzaTypes.map((pizzaRecipe) { + items: box.values.map((pizzaRecipe) { return DropdownMenuItem( value: pizzaRecipe.name, child: Text(pizzaRecipe.name) ); }).toList() - ) : CircularProgressIndicator() - ) + ) + ); + } + ), ] ), Row(