import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';

class loadFileCancelRequest{

  late Function progressCallback;
  late Function successCallback;

  CancelToken token = CancelToken();

  load(String url, String path) async {
    Dio dio = Dio();
    await dio.download(url, path, cancelToken: token, onReceiveProgress: (received, total) {
      if (total != -1) {
        // 下载进度
        progressCallback((received / total));
        debugPrint("检查更新-下载进度：${(received / total)}");
      }
    }).then((onValue){
      // 下载成功
      successCallback();
    });
  }

  cancel() {
    token.cancel('取消请求');
  }

  setProgressCallbackListener(Function progressCallback){
    this.progressCallback = progressCallback;
  }

  setSuccessCallbackListener(Function successCallback){
    this.successCallback = successCallback;
  }
}