void main() {
ALDownloader.initialize();
ALDownloader.configurePrint(true, frequentEnabled: false);
return runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: const Text('Material App Bar'),
),
body: ElevatedButton(
onPressed: () {
loadTest();
},
child: const Text("下载测试")),
),
);
}
Future<void> loadTest() async {
var url = "https://github.com/jackleemeta/al_downloader_flutter.git";
var directory = await getApplicationDocumentsDirectory();
ALDownloader.download(url,
directoryPath: "${directory.path}/data",
fileName: "fileName",
handlerInterface: ALDownloaderHandlerInterface(
progressHandler: (progress) {},
succeededHandler: () async {
// 下载成功
ALDownloader.remove(url);
var all = await ALDownloader.tasks;
var task = await ALDownloader.getTaskForUrl(url);
print("------------- 任务数量 ${all.length}");
print("------------- 任务链接 ${task?.url}");
},
failedHandler: () {},
pausedHandler: () {}));
}
}
static Future<void> _removeTask(ALDownloaderInnerTask task) async {
final taskId = task.taskId;
_addOrUpdateTaskForUrl(
task.url, taskId, ALDownloaderInnerStatus.deprecated, 0, null, null, task.waitingPhase);
task.willParameters = null;
task.headers = null;
task.redownloadIfNeeded = false;
task.isMayRedownloadAboutPause = false;
// 在这里 FlutterDownloader.loadTasks(); 有任务
var all = await FlutterDownloader.loadTasks();
if (taskId.length > 0)
await FlutterDownloader.remove(taskId: taskId, shouldDeleteContent: true);
// 在这里 FlutterDownloader.loadTasks(); 没任务,成功移除了。
var all2 = await FlutterDownloader.loadTasks();
}