From 8927136761f50843dea03090581690ff639406cd Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Thu, 9 Sep 2021 20:51:53 +0200 Subject: [PATCH] added sharing of recipes --- lib/pages/recipes_page.dart | 27 +++++++++++++++++++++++++++ pubspec.yaml | 4 ++++ todo.md | 1 - 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/pages/recipes_page.dart b/lib/pages/recipes_page.dart index e9d7c97..19ff329 100644 --- a/lib/pages/recipes_page.dart +++ b/lib/pages/recipes_page.dart @@ -1,9 +1,14 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/pizza_recipe.dart'; import 'package:pizzaplanner/pages/nav_drawer.dart'; import 'package:pizzaplanner/pages/scaffold.dart'; import 'package:pizzaplanner/widgets/pizza_recipe_widget.dart'; +import 'package:path/path.dart' as path; +import 'package:share_plus/share_plus.dart'; class RecipesPage extends StatefulWidget { @@ -64,6 +69,13 @@ class RecipesPageState extends State { title: Text(pizzaRecipe.name), content: const Text("What do you want to do?"), actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + sharePizzaRecipe(pizzaRecipe); + }, + child: const Text("Share"), + ), TextButton( onPressed: () { Navigator.pop(context); @@ -144,4 +156,19 @@ class RecipesPageState extends State { ), ); } + + Future sharePizzaRecipe(PizzaRecipe pizzaRecipe) async{ + final tempDir = await getTemporaryDirectory(); + final recipeName = pizzaRecipe.name.replaceAll(RegExp(r"\s+"), ""); + final recipeYamlPath = path.join( + tempDir.absolute.path, + "$recipeName.pizza" + ); + + final recipeYamlHandle = File(recipeYamlPath); + + await recipeYamlHandle.writeAsString(pizzaRecipe.toYaml()); + + Share.shareFiles([recipeYamlPath], text: "${pizzaRecipe.name}"); + } } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index d34a5ee..90050b3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -50,6 +50,10 @@ dependencies: flutter_ringtone_player: ^3.0.0 vibration: ^1.7.4-nullsafety.0 + path_provider: ^2.0.3 + path: ^1.8.0 + share_plus: ^2.1.4 + dev_dependencies: flutter_test: sdk: flutter diff --git a/todo.md b/todo.md index 1ca2b3c..6476c5f 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,6 @@ # TODO ## Feature -- share to/export of yaml of recipes - add an import of recipes from raw url - maybe allow a 'dir' yaml, that just points to other raw pizza yaml urls - add settings page