fixed up url pizza fetch page, with ValueNotifier

This commit is contained in:
broodjeaap89 2021-09-14 19:43:59 +02:00
parent 8cdfed1946
commit 2f821c7e09
2 changed files with 48 additions and 43 deletions

View file

@ -18,7 +18,7 @@ class AddRecipeURLPage extends StatefulWidget {
class AddRecipeURLPageState extends State<AddRecipeURLPage> { class AddRecipeURLPageState extends State<AddRecipeURLPage> {
String? url; String? url;
String tempUrl = "?"; String tempUrl = "?";
List<Widget> itemList = <Widget>[]; final ValueNotifier<List<Widget>> itemListNotifier = ValueNotifier(<Widget>[]);
@override @override
void initState() { void initState() {
@ -39,7 +39,7 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
width: double.infinity, width: double.infinity,
child: TextButton( child: TextButton(
onPressed: () async { onPressed: () async {
showDialog(context: context, builder: (BuildContext context) { await showDialog(context: context, builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
title: const Text("URL"), title: const Text("URL"),
content: TextFormField( content: TextFormField(
@ -48,9 +48,7 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
), ),
initialValue: url ?? "", initialValue: url ?? "",
onChanged: (String newUrl) { onChanged: (String newUrl) {
setState(() { tempUrl = newUrl;
tempUrl = newUrl;
});
}, },
), ),
actions: <Widget>[ actions: <Widget>[
@ -61,17 +59,17 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
child: const Text("Cancel"), child: const Text("Cancel"),
), ),
TextButton( TextButton(
onPressed: () { onPressed: () async {
Navigator.pop(context); Navigator.pop(context);
url = tempUrl; url = tempUrl;
setState(() { fetchUrl();
fetchUrl();
});
}, },
child: const Text("Fetch"), child: const Text("Fetch"),
), ),
], ],
); );
}).then((_) {
setState(() {});
}); });
}, },
child: Text(url ?? "Tap to load URL", style: const TextStyle(color: Colors.white)), child: Text(url ?? "Tap to load URL", style: const TextStyle(color: Colors.white)),
@ -81,8 +79,14 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
const Divider(), const Divider(),
Expanded( Expanded(
flex: 45, flex: 45,
child: ListView( child: ValueListenableBuilder<List<Widget>>(
children: itemList, valueListenable: itemListNotifier,
builder: (BuildContext context, List<Widget> widgets, Widget? child) {
print("test");
return ListView(
children: widgets
);
}
) )
) )
] ]
@ -105,39 +109,41 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
} }
final yamlBody = response.body; final yamlBody = response.body;
if (!(yamlBody.startsWith("recipe:") || yamlBody.startsWith("recipes"))){
return;
}
final pizzaRecipe = await PizzaRecipe.fromYaml(yamlBody); final pizzaRecipe = await PizzaRecipe.fromYaml(yamlBody);
itemList.clear(); itemListNotifier.value.clear();
itemList.add( itemListNotifier.value = <Widget>[ // inefficient probably but otherwise it doesn't trigger notify...
InkWell( InkWell(
onTap: () { onTap: () {
showDialog(context: context, builder: (BuildContext context) { showDialog(context: context, builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
title: Text(pizzaRecipe.name), title: Text(pizzaRecipe.name),
content: const Text("What do you want to do?"), content: const Text("What do you want to do?"),
actions: <Widget>[ actions: <Widget>[
TextButton( TextButton(
onPressed: () { onPressed: () {
Navigator.pop(context); Navigator.pop(context);
Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe); Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe);
}, },
child: const Text("View"), child: const Text("View"),
), ),
TextButton( TextButton(
onPressed: () { onPressed: () {
Navigator.pop(context); Navigator.pop(context);
addPizzaRecipeToBox(pizzaRecipe); addPizzaRecipeToBox(pizzaRecipe);
}, },
child: const Text("Add"), child: const Text("Add"),
), ),
] ]
); );
}); });
}, },
child: PizzaRecipeWidget(pizzaRecipe), child: PizzaRecipeWidget(pizzaRecipe),
) )
); ];
} catch (exception) { } catch (exception) {
print(exception); print(exception);
return; return;

View file

@ -13,7 +13,6 @@
- RecipeStep.waitUnit should probably be an enum? - RecipeStep.waitUnit should probably be an enum?
- refactor to const page names instead of loose strings everywhere ('/path/page') - refactor to const page names instead of loose strings everywhere ('/path/page')
- also do this with hive box names - also do this with hive box names
- probably use a stream for adding fetched url data to the listview ?
## Bug ## Bug
- add option to start recipe step instruction after step datetime and not completed - add option to start recipe step instruction after step datetime and not completed