Added in-app fileviewer for images and pdf files
This commit is contained in:
parent
6fda2f1078
commit
3d6e0cec0e
@ -11,6 +11,7 @@ import 'package:path_provider/path_provider.dart';
|
|||||||
import '../../../api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
|
import '../../../api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
|
||||||
import '../../../api/marianumcloud/webdav/webdavApi.dart';
|
import '../../../api/marianumcloud/webdav/webdavApi.dart';
|
||||||
import '../../../widget/confirmDialog.dart';
|
import '../../../widget/confirmDialog.dart';
|
||||||
|
import '../../../widget/fileViewer.dart';
|
||||||
import '../../../widget/unimplementedDialog.dart';
|
import '../../../widget/unimplementedDialog.dart';
|
||||||
import 'files.dart';
|
import 'files.dart';
|
||||||
|
|
||||||
@ -20,8 +21,8 @@ class FileElement extends StatefulWidget {
|
|||||||
final void Function() refetch;
|
final void Function() refetch;
|
||||||
const FileElement(this.file, this.path, this.refetch, {Key? key}) : super(key: key);
|
const FileElement(this.file, this.path, this.refetch, {Key? key}) : super(key: key);
|
||||||
|
|
||||||
static Future<DownloaderCore> download(String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async {
|
static Future<DownloaderCore> download(BuildContext context, String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async {
|
||||||
Directory paths = await getApplicationDocumentsDirectory();
|
Directory paths = await getTemporaryDirectory();
|
||||||
|
|
||||||
String local = paths.path + Platform.pathSeparator + name;
|
String local = paths.path + Platform.pathSeparator + name;
|
||||||
|
|
||||||
@ -34,11 +35,12 @@ class FileElement extends StatefulWidget {
|
|||||||
progress: ProgressImplementation(),
|
progress: ProgressImplementation(),
|
||||||
deleteOnCancel: true,
|
deleteOnCancel: true,
|
||||||
onDone: () {
|
onDone: () {
|
||||||
Future<OpenResult> result = OpenFile.open(local);
|
//Future<OpenResult> result = OpenFile.open(local); // TODO legacy - refactor remove
|
||||||
|
Navigator.of(context).push(MaterialPageRoute(builder: (context) => FileViewer(path: local)));
|
||||||
result.then((value) => {
|
onDone(OpenResult(message: "File viewer opened", type: ResultType.done));
|
||||||
onDone(value)
|
// result.then((value) => {
|
||||||
});
|
// onDone(value)
|
||||||
|
// });
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -125,7 +127,7 @@ class _FileElementState extends State<FileElement> {
|
|||||||
|
|
||||||
log("Download: ${widget.file.path} to ${widget.file.name}");
|
log("Download: ${widget.file.path} to ${widget.file.name}");
|
||||||
|
|
||||||
downloadCore = FileElement.download(widget.file.path, widget.file.name, (progress) {
|
downloadCore = FileElement.download(context, widget.file.path, widget.file.name, (progress) {
|
||||||
setState(() => percent = progress);
|
setState(() => percent = progress);
|
||||||
}, (result) {
|
}, (result) {
|
||||||
if(result.type != ResultType.done) {
|
if(result.type != ResultType.done) {
|
||||||
|
@ -235,7 +235,7 @@ class _ChatBubbleState extends State<ChatBubble> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
downloadProgress = 1;
|
downloadProgress = 1;
|
||||||
downloadCore = FileElement.download(message.file!.path!, message.file!.name, (progress) {
|
downloadCore = FileElement.download(context, message.file!.path!, message.file!.name, (progress) {
|
||||||
if(progress > 1) {
|
if(progress > 1) {
|
||||||
setState(() {
|
setState(() {
|
||||||
downloadProgress = progress;
|
downloadProgress = progress;
|
||||||
|
66
lib/widget/fileViewer.dart
Normal file
66
lib/widget/fileViewer.dart
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:better_open_file/better_open_file.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:photo_view/photo_view.dart';
|
||||||
|
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
|
||||||
|
|
||||||
|
import 'confirmDialog.dart';
|
||||||
|
import 'placeholderView.dart';
|
||||||
|
|
||||||
|
class FileViewer extends StatelessWidget {
|
||||||
|
final String path;
|
||||||
|
final bool openExternal;
|
||||||
|
const FileViewer({super.key, required this.path, this.openExternal = false});
|
||||||
|
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
AppBar appbar = AppBar(
|
||||||
|
title: Text(path.split("/").last),
|
||||||
|
actions: [
|
||||||
|
IconButton(onPressed: () => ConfirmDialog(
|
||||||
|
title: "Extern öffnen",
|
||||||
|
content: "Möchtest du die Datei mit dem Systemdialog öffnen?",
|
||||||
|
onConfirm: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => FileViewer(path: path, openExternal: true))),
|
||||||
|
confirmButton: "Öffnen",
|
||||||
|
).asDialog(context), icon: const Icon(Icons.open_in_new))
|
||||||
|
],
|
||||||
|
);
|
||||||
|
switch(openExternal ? "x" : path.split(".").last) {
|
||||||
|
case "png":
|
||||||
|
case "jpg":
|
||||||
|
case "jpeg":
|
||||||
|
return Scaffold(
|
||||||
|
appBar: appbar,
|
||||||
|
backgroundColor: Colors.white,
|
||||||
|
body: PhotoView(
|
||||||
|
imageProvider: Image.file(File(path)).image,
|
||||||
|
backgroundDecoration: const BoxDecoration(color: Colors.white60),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
case "pdf":
|
||||||
|
return Scaffold(
|
||||||
|
appBar: appbar,
|
||||||
|
body: SfPdfViewer.file(
|
||||||
|
File(path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
default:
|
||||||
|
OpenFile.open(path).then((result) {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
if(result.type != ResultType.done) {
|
||||||
|
showDialog(context: context, builder: (context) {
|
||||||
|
return AlertDialog(
|
||||||
|
content: Text(result.message),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return const PlaceholderView(text: "Datei wird extern geöffnet...", icon: Icons.open_in_new);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user