原文
https://medium.com/halkbank-m...
代码
https://github.com/deremakif/...
参考
- https://pub.dev/packages/path...
- https://pub.dev/packages/flut...
- https://pub.dev/packages/flowder
- https://pub.dev/packages/open...
- https://pub.dev/packages/perc...
注释
明天我要写一篇对于 flowder package 的文章。我用它从服务器上下载文件。有很多办法能够做到这一点,而且还有更受欢迎的软件包如 flutter_downloader 。但我更喜爱 flowder 软件包,因为它的实现很简略。
首先,如果下载文件夹不存在,咱们应该创立它。要做到这一点,咱们须要导入 path_provider package。并在当前页的 initState()
中调用 initPlatformState
办法。
Future<void> initPlatformState() async { _setPath(); if (!mounted) return;}void _setPath() async { Directory _path = await getApplicationDocumentsDirectory(); String _localPath = _path.path + Platform.pathSeparator + 'Download'; final savedDir = Directory(_localPath); bool hasExisted = await savedDir.exists(); if (!hasExisted) { savedDir.create(); } path = _localPath;}
当初,咱们有下载文件夹来保留文件。包的下载办法须要两个参数: URL 和选项。您能够依据须要自定义选项。
ElevatedButton( onPressed: () async { options = DownloaderUtils( progressCallback: (current, total) { final progress = (current / total) * 100; print('Downloading: $progress'); }, file: File('$path/loremipsum.pdf'), progress: ProgressImplementation(), onDone: () { OpenFile.open('$path/loremipsum.pdf'); }, deleteOnCancel: true, ); core = await Flowder.download( "https://assets.website-files.com/603d0d2db8ec32ba7d44fffe/603d0e327eb2748c8ab1053f_loremipsum.pdf", options, );},
我应用 OpenFile package 包在文件实现下载过程时关上它。我还应用了 percent_indicator package 包来显示停顿。
如果当前不须要应用该文件,能够在敞开文档后删除该文件。重要的是不要减少应用程序的大小。
OpenFile.open('$path/loremipsum.pdf').then((value) { File f = File('$path/loremipsum.pdf'); f.delete();});
- 应用程序演示
示例我的项目的源代码。
- GitHub - deremakif/FlowderSample
https://github.com/deremakif/...
© 猫哥
- https://ducafecat.tech/
- https://github.com/ducafecat
- 微信群 ducafecat