一个基于url的Flutter下载器,支持下载任意类型的文件,并自动管理下载相关的各种事务。
- 通过url管理下载任务
- 简单的下载状态
- 不频繁地I/O
- 提供便利的下载句柄
- 支持批量下载
- 自动管理文件,不需要指定下载路径
- 基于flutter_downloader
原生配置:和flutter_downloader相同
添加下面这行代码到pubspec.yaml中
dependencies:
al_downloader: ^1.8.4
使用命令行运行下面这行代码
flutter packages get
引入下面这行代码来使用al_downloader
import 'package:al_downloader/al_downloader.dart';
ALDownloader.initialize();
ALDownloader.configurePrint(true, frequentEnabled: false);
ALDownloader.download(url,
directoryPath: directoryPath,
fileName: fileName,
handlerInterface:
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}));
ALDownloader.addHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}),
url);
ALDownloader.addForeverHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}),
url);
ALDownloader.removeHandlerInterfaceForUrl(url);
/// 停止下载,不删除未下载完成的数据
ALDownloader.pause(url);
/// 停止下载,删除未下载完成的数据
ALDownloader.cancel(url);
/// 删除下载,删除所有数据
ALDownloader.remove(url);
final status = await ALDownloader.getStatusForUrl(url);
final progress = await ALDownloader.getProgressForUrl(url);
final task = await ALDownloader.getTaskForUrl(url);
final tasks = await ALDownloader.tasks;
ALDownloaderBatcher.download(urls,
handlerInterface:
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | 批量 | 下载进度 = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 批量 | 下载成功\n');
}, failedHandler: () {
debugPrint('ALDownloader | 批量 | 下载失败\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 批量 | 下载暂停\n');
}));
ALDownloaderBatcher.addHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | 批量 | 下载进度 = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 批量 | 下载成功\n');
}, failedHandler: () {
debugPrint('ALDownloader | 批量 | 下载失败\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 批量 | 下载暂停\n');
}),
urls);
final status = ALDownloaderBatcher.getStatusForUrls(urls);
final tasks = await ALDownloaderBatcher.getTasksForUrls(urls);
final physicalFilePath =
await ALDownloaderFileManager.getPhysicalFilePathForUrl(url);
debugPrint(
'ALDownloader | 获取[url]的物理文件路径, url = $url, path = $physicalFilePath\n');
1. 如果持久化文件被一些异常方式删除了,比如某些业务代码删除了缓存文件夹,调用[remove],然后调用[download]重新下载来解决这个问题
欢迎贡献!
如果你有任何问题、建议或想法,欢迎随时提出issue或PR!
Maintainer: jackleemeta (jackleemeta@outlook.com)