moved all the pizza event page 'build' methods in the entities to seperate stateful widgets

flutter gets annoying with many nested stateful things...
This commit is contained in:
broodjeaap89 2021-08-03 20:55:30 +02:00
parent 3e043eccb4
commit 3c28bd01ca
4 changed files with 154 additions and 147 deletions

View file

@ -61,12 +61,6 @@ class PizzaRecipe extends HiveObject {
return stepCount;
}
Widget getPizzaEventRecipeWidget(BuildContext context, PizzaEventPageState pizzaEventPage) {
return ListView(
children: this.recipeSteps.map((recipeStep) => recipeStep.buildPizzaEventRecipeStepWidget(context, pizzaEventPage)).toList()
);
}
static Future<PizzaRecipe> fromYaml(yamlPath) async{
String yamlString = await loadAsset(yamlPath);
var yaml = loadYaml(yamlString);

View file

@ -47,68 +47,6 @@ class RecipeStep extends HiveObject {
completedOn != null;
}
Widget buildPizzaEventRecipeStepWidget(BuildContext context, PizzaEventPageState pizzaEventPage){
return this.subSteps.length > 0 ?
buildPizzaEventRecipeStepWidgetWithSubSteps(context, pizzaEventPage) :
buildPizzaEventRecipeStepWidgetWithoutSubSteps(context, pizzaEventPage);
}
Widget buildPizzaEventRecipeStepWidgetWithSubSteps(BuildContext context, PizzaEventPageState pizzaEventPage) {
int recipeSubStepsCompleted = this.subSteps.where((subStep) => subStep.completed).length;
int recipeSubSteps = this.subSteps.length;
return ExpansionTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.name),
Text("$recipeSubStepsCompleted/$recipeSubSteps")
],
),
children: <Widget>[
Text(this.description),
] + subSteps.map((subStep) => subStep.buildTest(context, pizzaEventPage)).toList() //subStep.buildPizzaEventSubStepWidget(context, pizzaEventPage)).toList()
);
}
Widget buildPizzaEventRecipeStepWidgetWithoutSubSteps(BuildContext context, PizzaEventPageState pizzaEventPage) {
return ExpansionTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.name),
Text("${this.completedOn == null ? 0 : 1}/1")
],
),
children: <Widget>[
Text(this.description),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(this.name),
Checkbox(
value: this.completedOn != null,
onChanged: (bool? newValue) async {
if (newValue == null){
return;
}
if (newValue){
this.completedOn = DateTime.now();
} else {
this.completedOn = null;
}
await pizzaEventPage.widget.pizzaEvent.save();
pizzaEventPage.triggerSetState();
},
)
],
)
]
);
}
int convertToSeconds(int value){
switch (waitUnit){
case "minutes": {

View file

@ -19,82 +19,4 @@ class RecipeSubStep extends HiveObject {
bool get completed => completedOn != null;
RecipeSubStep(this.name, this.description);
Widget buildPizzaEventSubStepWidget(BuildContext context, PizzaEventPageState pizzaEventPage) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(this.name),
Checkbox(
value: this.completed,
onChanged: (bool? newValue) async {
if (newValue == null){
return;
}
if (newValue){
this.completedOn = DateTime.now();
} else {
this.completedOn = null;
}
await pizzaEventPage.widget.pizzaEvent.save();
pizzaEventPage.triggerSetState();
},
)
],
);
}
Widget buildTest(BuildContext context, PizzaEventPageState pizzaEventPage){
return InkWell(
onTap: () {
showDialog(
context: context,
builder: (context) {
return getDialog(context);
}
);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(this.name),
Checkbox(
value: this.completed,
onChanged: (b) {},
)
],
),
);
}
Widget getDialog(BuildContext context){
return Dialog(
insetPadding: EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text(this.name),
Text(this.description),
Expanded(
child: Container()
),
SizedBox(
width: double.infinity,
height: 70,
child: Container(
color: this.completed ? Colors.green : Colors.redAccent,
child: TextButton(
child: Text(this.completed ? "Complete" : "Todo", style: TextStyle(color: Colors.white)),
onPressed: () {
this.completedOn = this.completed ? null : DateTime.now();
},
)
)
)
]
)
)
);
}
}

View file

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:pizzaplanner/entities/PizzaEvent.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
class PizzaEventPage extends StatefulWidget {
PizzaEvent pizzaEvent;
@ -21,10 +23,161 @@ class PizzaEventPageState extends State<PizzaEventPage> {
resizeToAvoidBottomInset: false,
body: Container(
padding: EdgeInsets.all(10),
child: this.widget.pizzaEvent.recipe.getPizzaEventRecipeWidget(context, this)
child: ListView(
children: this.widget.pizzaEvent.recipe.recipeSteps.map((recipeStep) => PizzaEventRecipeStepWidget(recipeStep)).toList()
)
)
);
}
triggerSetState() => setState(() {});
}
class PizzaEventRecipeStepWidget extends StatefulWidget {
final RecipeStep recipeStep;
PizzaEventRecipeStepWidget(this.recipeStep);
PizzaEventRecipeStepWidgetState createState() => PizzaEventRecipeStepWidgetState();
}
class PizzaEventRecipeStepWidgetState extends State<PizzaEventRecipeStepWidget> {
@override
Widget build(BuildContext context) {
return this.widget.recipeStep.subSteps.length > 0 ?
buildPizzaEventRecipeStepWidgetWithSubSteps() :
buildPizzaEventRecipeStepWidgetWithoutSubSteps();
}
Widget buildPizzaEventRecipeStepWidgetWithSubSteps() {
int recipeSubStepsCompleted = this.widget.recipeStep.subSteps.where((subStep) => subStep.completed).length;
int recipeSubSteps = this.widget.recipeStep.subSteps.length;
return ExpansionTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.widget.recipeStep.name),
Text("$recipeSubStepsCompleted/$recipeSubSteps")
],
),
children: <Widget>[
Text(this.widget.recipeStep.description),
] + this.widget.recipeStep.subSteps.map((subStep) => PizzaEventSubStepWidget(subStep)).toList() //subStep.buildPizzaEventSubStepWidget(context, pizzaEventPage)).toList()
);
}
Widget buildPizzaEventRecipeStepWidgetWithoutSubSteps() {
return ExpansionTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.widget.recipeStep.name),
Text("${this.widget.recipeStep.completedOn == null ? 0 : 1}/1")
],
),
children: <Widget>[
Text(this.widget.recipeStep.description),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(this.widget.recipeStep.name),
Checkbox(
value: this.widget.recipeStep.completedOn != null,
onChanged: (bool? newValue) async {
if (newValue == null){
return;
}
this.widget.recipeStep.completedOn = newValue ? DateTime.now() : null;
setState(() {});
},
)
],
)
]
);
}
}
class PizzaEventSubStepWidget extends StatefulWidget {
final RecipeSubStep recipeSubStep;
PizzaEventSubStepWidget(this.recipeSubStep);
PizzaEventSubStepWidgetState createState() => PizzaEventSubStepWidgetState();
}
class PizzaEventSubStepWidgetState extends State<PizzaEventSubStepWidget> {
@override
Widget build(BuildContext context){
return InkWell(
onTap: () async {
await showDialog(
context: context,
builder: (context) {
return PizzaEventSubStepDialog(this.widget.recipeSubStep);
}
);
setState(() {});
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(this.widget.recipeSubStep.name),
IgnorePointer(
child: Checkbox(
value: this.widget.recipeSubStep.completed,
onChanged: (b) {},
)
)
],
),
);
}
}
class PizzaEventSubStepDialog extends StatefulWidget {
final RecipeSubStep recipeSubStep;
PizzaEventSubStepDialog(this.recipeSubStep);
PizzaEventSubStepDialogState createState() => PizzaEventSubStepDialogState();
}
class PizzaEventSubStepDialogState extends State<PizzaEventSubStepDialog> {
@override
Widget build(BuildContext context) {
return Dialog(
insetPadding: EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text(this.widget.recipeSubStep.name),
Text(this.widget.recipeSubStep.description),
Expanded(
child: Container()
),
SizedBox(
width: double.infinity,
height: 70,
child: Container(
color: this.widget.recipeSubStep.completed ? Colors.green : Colors.redAccent,
child: TextButton(
child: Text(this.widget.recipeSubStep.completed ? "Complete" : "Todo", style: TextStyle(color: Colors.white)),
onPressed: () {
setState(() {
this.widget.recipeSubStep.completedOn = this.widget.recipeSubStep.completed ? null : DateTime.now();
});
},
)
)
)
]
)
)
);
}
}