Merge pull request 'develop-fileDownload' (#92) from develop-fileDownload into develop
Reviewed-on: #92 Reviewed-by: Elias Müller <elias@elias-mueller.com>
This commit was merged in pull request #92.
This commit is contained in:
23
lib/utils/FileSaver.dart
Normal file
23
lib/utils/FileSaver.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
// only tested on android!
|
||||
class FileSaver {
|
||||
static Future<String> getExternalDocumentPath() async {
|
||||
var permission = await Permission.storage.status;
|
||||
if(!permission.isGranted) {
|
||||
await Permission.storage.request();
|
||||
}
|
||||
var directory = Directory('/storage/emulated/0/Download');
|
||||
final externalPath = directory.path;
|
||||
await Directory(externalPath).create(recursive: true);
|
||||
return externalPath;
|
||||
}
|
||||
|
||||
static Future<File> writeBytes(List<int> bytes, String name) async {
|
||||
final path = await getExternalDocumentPath();
|
||||
var file = File('$path/$name');
|
||||
return file.writeAsBytes(bytes);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import 'package:share_plus/share_plus.dart';
|
||||
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
|
||||
|
||||
import '../storage/base/settingsProvider.dart';
|
||||
import '../utils/FileSaver.dart';
|
||||
import 'infoDialog.dart';
|
||||
import 'placeholderView.dart';
|
||||
import 'sharePositionOrigin.dart';
|
||||
|
||||
@@ -21,6 +23,12 @@ class FileViewer extends StatefulWidget {
|
||||
State<FileViewer> createState() => _FileViewerState();
|
||||
}
|
||||
|
||||
enum FileViewingActions {
|
||||
openExternal,
|
||||
share,
|
||||
save
|
||||
}
|
||||
|
||||
class _FileViewerState extends State<FileViewer> {
|
||||
PhotoViewController photoViewController = PhotoViewController();
|
||||
|
||||
@@ -38,22 +46,57 @@ class _FileViewerState extends State<FileViewer> {
|
||||
AppBar appbar({List actions = const []}) => AppBar(
|
||||
title: Text(widget.path.split('/').last),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
...actions,
|
||||
PopupMenuButton<FileViewingActions>(
|
||||
onSelected: (value) async {
|
||||
switch(value) {
|
||||
case FileViewingActions.openExternal:
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => FileViewer(path: widget.path, openExternal: true))
|
||||
),
|
||||
icon: const Icon(Icons.open_in_new)
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
Share.shareXFiles(
|
||||
[XFile(widget.path)],
|
||||
sharePositionOrigin: SharePositionOrigin.get(context),
|
||||
);
|
||||
break;
|
||||
case FileViewingActions.share:
|
||||
SharePlus.instance.share(
|
||||
ShareParams(
|
||||
files: [XFile(widget.path)],
|
||||
sharePositionOrigin: SharePositionOrigin.get(context)
|
||||
)
|
||||
);
|
||||
break;
|
||||
case FileViewingActions.save:
|
||||
await FileSaver.writeBytes(await File(widget.path).readAsBytes(), widget.path.split('/').last);
|
||||
if(!context.mounted) return;
|
||||
InfoDialog.show(context, 'Die Datei wurde im Downloads Ordner gespeichert.');
|
||||
break;
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.share_outlined),
|
||||
itemBuilder: (context) => <PopupMenuEntry<FileViewingActions>>[
|
||||
const PopupMenuItem(
|
||||
value: FileViewingActions.openExternal,
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.open_in_new),
|
||||
title: Text('Extern öffnen'),
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: FileViewingActions.share,
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.share_outlined),
|
||||
title: Text('Teilen'),
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
if(Platform.isAndroid) const PopupMenuItem(
|
||||
value: FileViewingActions.save,
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.save_alt_outlined),
|
||||
title: Text('Speichern'),
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
...actions
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ dependencies:
|
||||
open_filex: ^4.7.0
|
||||
package_info_plus: ^8.1.3
|
||||
path_provider: ^2.1.5
|
||||
permission_handler: ^12.0.1
|
||||
persistent_bottom_nav_bar_v2: ^6.1.0
|
||||
photo_view: ^0.15.0
|
||||
pretty_json: ^2.0.0
|
||||
|
||||
Reference in New Issue
Block a user