From 2f821c7e094fa3621ceca1e74ff359a2ef88522f Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Tue, 14 Sep 2021 19:43:59 +0200 Subject: [PATCH] fixed up url pizza fetch page, with ValueNotifier --- lib/pages/add_recipe_url.dart | 90 +++++++++++++++++++---------------- todo.md | 1 - 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/pages/add_recipe_url.dart b/lib/pages/add_recipe_url.dart index 9d7eec8..ad1a6da 100644 --- a/lib/pages/add_recipe_url.dart +++ b/lib/pages/add_recipe_url.dart @@ -18,7 +18,7 @@ class AddRecipeURLPage extends StatefulWidget { class AddRecipeURLPageState extends State { String? url; String tempUrl = "?"; - List itemList = []; + final ValueNotifier> itemListNotifier = ValueNotifier([]); @override void initState() { @@ -39,7 +39,7 @@ class AddRecipeURLPageState extends State { width: double.infinity, child: TextButton( onPressed: () async { - showDialog(context: context, builder: (BuildContext context) { + await showDialog(context: context, builder: (BuildContext context) { return AlertDialog( title: const Text("URL"), content: TextFormField( @@ -48,9 +48,7 @@ class AddRecipeURLPageState extends State { ), initialValue: url ?? "", onChanged: (String newUrl) { - setState(() { - tempUrl = newUrl; - }); + tempUrl = newUrl; }, ), actions: [ @@ -61,17 +59,17 @@ class AddRecipeURLPageState extends State { child: const Text("Cancel"), ), TextButton( - onPressed: () { + onPressed: () async { Navigator.pop(context); url = tempUrl; - setState(() { - fetchUrl(); - }); + fetchUrl(); }, child: const Text("Fetch"), ), ], ); + }).then((_) { + setState(() {}); }); }, child: Text(url ?? "Tap to load URL", style: const TextStyle(color: Colors.white)), @@ -81,8 +79,14 @@ class AddRecipeURLPageState extends State { const Divider(), Expanded( flex: 45, - child: ListView( - children: itemList, + child: ValueListenableBuilder>( + valueListenable: itemListNotifier, + builder: (BuildContext context, List widgets, Widget? child) { + print("test"); + return ListView( + children: widgets + ); + } ) ) ] @@ -105,39 +109,41 @@ class AddRecipeURLPageState extends State { } final yamlBody = response.body; + if (!(yamlBody.startsWith("recipe:") || yamlBody.startsWith("recipes"))){ + return; + } final pizzaRecipe = await PizzaRecipe.fromYaml(yamlBody); - - itemList.clear(); - itemList.add( - InkWell( - onTap: () { - showDialog(context: context, builder: (BuildContext context) { - return AlertDialog( - title: Text(pizzaRecipe.name), - content: const Text("What do you want to do?"), - actions: [ - TextButton( - onPressed: () { - Navigator.pop(context); - Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe); - }, - child: const Text("View"), - ), - TextButton( - onPressed: () { - Navigator.pop(context); - addPizzaRecipeToBox(pizzaRecipe); - }, - child: const Text("Add"), - ), - ] - ); - }); - }, - child: PizzaRecipeWidget(pizzaRecipe), - ) - ); + itemListNotifier.value.clear(); + itemListNotifier.value = [ // inefficient probably but otherwise it doesn't trigger notify... + InkWell( + onTap: () { + showDialog(context: context, builder: (BuildContext context) { + return AlertDialog( + title: Text(pizzaRecipe.name), + content: const Text("What do you want to do?"), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe); + }, + child: const Text("View"), + ), + TextButton( + onPressed: () { + Navigator.pop(context); + addPizzaRecipeToBox(pizzaRecipe); + }, + child: const Text("Add"), + ), + ] + ); + }); + }, + child: PizzaRecipeWidget(pizzaRecipe), + ) + ]; } catch (exception) { print(exception); return; diff --git a/todo.md b/todo.md index 32a1b76..31dc387 100644 --- a/todo.md +++ b/todo.md @@ -13,7 +13,6 @@ - RecipeStep.waitUnit should probably be an enum? - refactor to const page names instead of loose strings everywhere ('/path/page') - also do this with hive box names -- probably use a stream for adding fetched url data to the listview ? ## Bug - add option to start recipe step instruction after step datetime and not completed