switching dropdown on add pizza event page to pizza recipe box

This commit is contained in:
broodjeaap89 2021-07-26 21:50:10 +02:00
parent f417445259
commit 621a863f45

View file

@ -2,11 +2,14 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:hive/hive.dart';
import 'package:pizzaplanner/entities/PizzaEvent.dart'; import 'package:pizzaplanner/entities/PizzaEvent.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
import 'package:pizzaplanner/util.dart'; import 'package:pizzaplanner/util.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class AddPizzaEventPage extends StatefulWidget { class AddPizzaEventPage extends StatefulWidget {
@override @override
AddPizzaEventPageState createState() => AddPizzaEventPageState(); AddPizzaEventPageState createState() => AddPizzaEventPageState();
@ -16,27 +19,12 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
String name = ""; String name = "";
bool initialized = false; bool initialized = false;
late PizzaRecipe pizzaRecipe; late PizzaRecipe pizzaRecipe;
late List<PizzaRecipe> pizzaTypes;
int pizzaCount = 1; int pizzaCount = 1;
int doughBallSize = 250; int doughBallSize = 250;
DateTime eventTime = DateTime.now(); DateTime eventTime = DateTime.now();
bool nameValidation = false; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -75,21 +63,29 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
children: <Widget>[ children: <Widget>[
Icon(FontAwesome5.pizza_slice), Icon(FontAwesome5.pizza_slice),
Container(width: 25), Container(width: 25),
Expanded( ValueListenableBuilder(
child: this.initialized ? // Only render the dropdown if the recipes have been loaded from storage valueListenable: Hive.box<PizzaRecipe>("PizzaRecipes").listenable(),
DropdownButton<String>( builder: (context, Box<PizzaRecipe> box, widget) {
if (box.isEmpty){
return Text("Loading Pizza Recipes...");
}
this.pizzaRecipe = box.values.first;
return Expanded(
child: DropdownButton<String>(
value: this.pizzaRecipe.name, value: this.pizzaRecipe.name,
onChanged: (String? newType) { 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( return DropdownMenuItem(
value: pizzaRecipe.name, value: pizzaRecipe.name,
child: Text(pizzaRecipe.name) child: Text(pizzaRecipe.name)
); );
}).toList() }).toList()
) : CircularProgressIndicator() )
) );
}
),
] ]
), ),
Row( Row(