import 'dart:developer'; import 'dart:io'; import 'package:better_open_file/better_open_file.dart'; import 'package:flowder/flowder.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/webdavApi.dart'; import 'package:path_provider/path_provider.dart'; class FileDownload extends StatefulWidget { String source; String name; FileDownload(this.source, this.name, {Key? key}) : super(key: key); @override State createState() => _FileDownloadState(); } class _FileDownloadState extends State { late DownloaderCore core; void download(String remotePath, String name) async { Directory paths = await getApplicationDocumentsDirectory(); String local = paths.path + Platform.pathSeparator + name; DownloaderUtils options = DownloaderUtils( progressCallback: (current, total) { final progress = (current / total) * 100; setState(() => { percent = progress, }); }, file: File(local), progress: ProgressImplementation(), deleteOnCancel: true, onDone: () { Navigator.of(context).pop(); showDialog(context: context, builder: (context) { return AlertDialog( icon: const Icon(Icons.link), title: const Text("Dateipfad"), content: Text(local), actions: [ TextButton( child: Text("Öffnen mit"), onPressed: () { OpenFile.open(local).then((value) => () { log("Result: ${value.toString()}"); }); }, ) ], ); }); }, ); log(local); core = await Flowder.download( "${await WebdavApi.webdavConnectString}$remotePath", options, ); } double percent = 0; @override void initState() { super.initState(); log("Downloading file: Source(${widget.source}), Name(${widget.name})"); download(widget.source, widget.name); } @override Widget build(BuildContext context) { return AlertDialog( title: const Text("Download"), content: ListTile( leading: const Icon(Icons.download), title: LinearProgressIndicator(value: percent/100), trailing: Text("${percent.round()}%"), ), actions: [ TextButton( child: const Text("Abbrechen"), onPressed: () => () { if(!core.isCancelled) core.cancel(); Navigator.of(context, rootNavigator: true).pop(); }, ) ], ); } }