feat: export JSON in mobile and update mobile_example.json (#515)

* feat: add export json in mobile

* chore: update editor style and example json for mobile showcase

* chore: improve text content
This commit is contained in:
Yijing Huang 2023-10-10 00:42:51 -07:00 • committed by GitHub
commit 897033035c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_html/html.dart' as html;
enum ExportFileType {
@ -290,7 +291,31 @@ class _HomePageState extends State<HomePage> {
throw UnimplementedError();
}
if (!kIsWeb) {
if (kIsWeb) {
final blob = html.Blob([result], 'text/plain', 'native');
html.AnchorElement(
href: html.Url.createObjectUrlFromBlob(blob).toString(),
)
..setAttribute('download', 'document.${fileType.extension}')
..click();
} else if (PlatformExtension.isMobile) {
final appStorageDirectory = await getApplicationDocumentsDirectory();
final path = File(
'${appStorageDirectory.path}/${DateTime.now()}.${fileType.extension}',
);
await path.writeAsString(result);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'This document is saved to the ${appStorageDirectory.path}',
),
),
);
}
} else {
// for desktop
final path = await FilePicker.platform.saveFile(
fileName: 'document.${fileType.extension}',
);
@ -304,13 +329,6 @@ class _HomePageState extends State<HomePage> {
);
}
}
} else {
final blob = html.Blob([result], 'text/plain', 'native');
html.AnchorElement(
href: html.Url.createObjectUrlFromBlob(blob).toString(),
)
..setAttribute('download', 'document.${fileType.extension}')
..click();
}
}