From 93bb696988530949ca8ed6ec9d00f08978801ada Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Thu, 9 Sep 2021 21:47:27 +0200 Subject: [PATCH] added loading of pizza yaml from local storage --- lib/pages/recipes_page.dart | 63 +++++++++++++++++++++++++++++++------ pubspec.yaml | 2 ++ todo.md | 1 - 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/lib/pages/recipes_page.dart b/lib/pages/recipes_page.dart index 19ff329..2951023 100644 --- a/lib/pages/recipes_page.dart +++ b/lib/pages/recipes_page.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:path_provider/path_provider.dart'; @@ -142,21 +143,65 @@ class RecipesPageState extends State { ), ), const Divider(), - Container( - color: Colors.blue, - width: double.infinity, - child: TextButton( - onPressed: () async { - Navigator.pushNamed(context, "/recipes/edit"); - }, - child: const Text("New Recipe", style: TextStyle(color: Colors.white)), - ) + Expanded( + flex: 5, + child: Row( + children: [ + Expanded( + flex: 5, + child: Container( + color: Colors.blue, + width: double.infinity, + child: TextButton( + onPressed: () async { + loadRecipe(); + }, + child: const Text("Load Recipe", style: TextStyle(color: Colors.white)), + ) + ), + ), + const Expanded( + flex: 1, + child: SizedBox(), + ), + Expanded( + flex: 5, + child: Container( + color: Colors.blue, + width: double.infinity, + child: TextButton( + onPressed: () async { + Navigator.pushNamed(context, "/recipes/edit"); + }, + child: const Text("New Recipe", style: TextStyle(color: Colors.white)), + ) + ), + ) + ], + ) ), ] ), ); } + Future loadRecipe() async { + final result = await FilePicker.platform.pickFiles(); + if (result == null){ + return; + } + + try { + File(result.files.single.path).readAsString().then((String contents) async { + final pizzaRecipe = await PizzaRecipe.fromYaml(contents); + final pizzaRecipeBox = Hive.box("PizzaRecipes"); + pizzaRecipeBox.add(pizzaRecipe); + }); + } catch (exception) { + print(exception); + } + } + Future sharePizzaRecipe(PizzaRecipe pizzaRecipe) async{ final tempDir = await getTemporaryDirectory(); final recipeName = pizzaRecipe.name.replaceAll(RegExp(r"\s+"), ""); diff --git a/pubspec.yaml b/pubspec.yaml index 90050b3..e053913 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -54,6 +54,8 @@ dependencies: path: ^1.8.0 share_plus: ^2.1.4 + file_picker: ^4.0.2 + dev_dependencies: flutter_test: sdk: flutter diff --git a/todo.md b/todo.md index c5f9a23..850df12 100644 --- a/todo.md +++ b/todo.md @@ -3,7 +3,6 @@ ## Feature - add 'capturing' sharing intent - add import of recipes - - from local - from raw url - maybe allow a 'dir' yaml, that just points to other raw pizza yaml urls - add settings page