downloadApkHandler method

Future downloadApkHandler(
  1. String url,
  2. String path
)

Implementation

Future downloadApkHandler(String url, String path) async {
  final isDoing = _downloadStatus == DownloadStatus.downloading;
  final isStart = _downloadStatus == DownloadStatus.start;
  final isDone = _downloadStatus == DownloadStatus.done;

  if (isDoing || isStart || isDone) {
    debugPrint('当前下载状态:$_downloadStatus, 不能重复下载。');
    return;
  }

  // start
  _downloadStatus = DownloadStatus.start;
  widget.downloadStatusChange?.call(_downloadStatus, error: null);

  try {
    await Dio().download(
      url,
      path,
      options: Options(headers: widget.headers),
      onReceiveProgress: (int count, int total) {
        if (total == -1) {
          _downloadProgress = 0.01;
        } else {
          widget.downloadProgress?.call(count, total);
          _downloadProgress = count / total.toDouble();
        }

        setState(() {});

        if (_downloadProgress == 1) {
          _downloadStatus = DownloadStatus.done;
          widget.downloadStatusChange?.call(DownloadStatus.done);
          FlutterUpgradeChanneler.installAppForAndroid(path);
          Navigator.pop(context);
        }
      },
    );
  } catch (e) {
    _downloadProgress = 0;
    _downloadStatus = DownloadStatus.error;
    widget.downloadStatusChange?.call(DownloadStatus.error, error: e);
  }
}