added loading of pizza yaml from local storage

This commit is contained in:
broodjeaap89 2021-09-09 21:47:27 +02:00
parent ed21926cae
commit 93bb696988
3 changed files with 56 additions and 10 deletions

View file

@ -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<RecipesPage> {
),
),
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: <Widget>[
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<void> 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<PizzaRecipe>("PizzaRecipes");
pizzaRecipeBox.add(pizzaRecipe);
});
} catch (exception) {
print(exception);
}
}
Future<void> sharePizzaRecipe(PizzaRecipe pizzaRecipe) async{
final tempDir = await getTemporaryDirectory();
final recipeName = pizzaRecipe.name.replaceAll(RegExp(r"\s+"), "");

View file

@ -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

View file

@ -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