如何取消下载文件?

发布于 2025-02-09 13:27:05 字数 2897 浏览 2 评论 0原文

我有将文件下载到Android上的本地存储的方法。我必须有机会取消此加载过程。我该怎么做? 我将集团与事件一起使用。这是我的事件功能:

Future saveFile(Emitter<FileState> emit, FileModel fileToDownload) async {
emit(_loadingState());
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/${fileToDownload.name}');
final ref = FirebaseStorage.instance.ref('uploads/${fileToDownload.name}');
final task = ref.writeToFile(file);
final exists = await file.exists();
emit(
  FileState(
    status: BlocStatus.loaded,
    exists: exists,
    task: task,
  ),
);

}

我知道有一种可以取消此过程的方法-task.cancel();

但是我必须在哪里写呢?我该如何实施呢?在UI部分中,我有按钮(getureTeTector -ontap-在圆形percentIndicator中),其中应取消功能。这是代码:

return BlocBuilder<FileBloc, FileState>(
  bloc: fileBloc,
  builder: (context, state) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      alignment: Alignment.centerLeft,
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: 12.w,
          vertical: 8.h,
        ),
        decoration: BoxDecoration(
          color: Palette.lightGray,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.r),
            topRight: Radius.circular(10.r),
            bottomRight: Radius.circular(10.r),
          ),
        ),
        width: 263.w,
        height: 56.h,
        child: Row(
          children: [
            GestureDetector(
              onTap: () async {
                if (!state.exists) {
                  fileBloc.add(SaveFileEvent(widget.message.file!));
                }
              },
              child: SizedBox(
                height: 40.h,
                width: 32.w,
                child: state.status == BlocStatus.loading
                    ? CircularPercentIndicator(
                        radius: 16.r,
                        animation: true,
                        lineWidth: 2.w,
                        percent: 1,
                        circularStrokeCap: CircularStrokeCap.round,
                        progressColor: Palette.green,
                        center: GestureDetector(
                          onTap: () {
                            //here should be cancel functionality
                          },
                          child: SizedBox(
                            height: 10.5.h,
                            width: 10.5.w,
                            child: SvgPicture.asset(
                              'assets/icons/cancel.svg',
                              color: Palette.white,
                            ),
                          ),
                        ),
                      )
                    : state.exists
                        ? Image.asset('assets/images/saved-file.png')
                        : Image.asset('assets/images/non-saved-file.png'),
              ),
            ),

I have the method which downloads my File to local storage on Android. I have to give the opportunity to cancel this loading process. How can I do it?
I use bloc with events. Here is my function for event:

Future saveFile(Emitter<FileState> emit, FileModel fileToDownload) async {
emit(_loadingState());
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/${fileToDownload.name}');
final ref = FirebaseStorage.instance.ref('uploads/${fileToDownload.name}');
final task = ref.writeToFile(file);
final exists = await file.exists();
emit(
  FileState(
    status: BlocStatus.loaded,
    exists: exists,
    task: task,
  ),
);

}

I know that there exists a method that can cancel this process - task.cancel();

But where do I have to write it? And how can I have to implement it? In the UI part I have button (GestureDetector - onTap - in CircularPercentIndicator), where should be Cancel functionality. Here is the code:

return BlocBuilder<FileBloc, FileState>(
  bloc: fileBloc,
  builder: (context, state) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      alignment: Alignment.centerLeft,
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: 12.w,
          vertical: 8.h,
        ),
        decoration: BoxDecoration(
          color: Palette.lightGray,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.r),
            topRight: Radius.circular(10.r),
            bottomRight: Radius.circular(10.r),
          ),
        ),
        width: 263.w,
        height: 56.h,
        child: Row(
          children: [
            GestureDetector(
              onTap: () async {
                if (!state.exists) {
                  fileBloc.add(SaveFileEvent(widget.message.file!));
                }
              },
              child: SizedBox(
                height: 40.h,
                width: 32.w,
                child: state.status == BlocStatus.loading
                    ? CircularPercentIndicator(
                        radius: 16.r,
                        animation: true,
                        lineWidth: 2.w,
                        percent: 1,
                        circularStrokeCap: CircularStrokeCap.round,
                        progressColor: Palette.green,
                        center: GestureDetector(
                          onTap: () {
                            //here should be cancel functionality
                          },
                          child: SizedBox(
                            height: 10.5.h,
                            width: 10.5.w,
                            child: SvgPicture.asset(
                              'assets/icons/cancel.svg',
                              color: Palette.white,
                            ),
                          ),
                        ),
                      )
                    : state.exists
                        ? Image.asset('assets/images/saved-file.png')
                        : Image.asset('assets/images/non-saved-file.png'),
              ),
            ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2025-02-16 13:27:05

是的,它有效。我为集团添加了一个新领域:
延迟下载任务?任务;
在加载过程中,在保存方法中加载时初始化它,然后在取消方法中使用它。
这是方法:

 Future saveFile(FileModel fileToDownload) async {
emit(_loadingState());
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/${fileToDownload.name}');
final ref = FirebaseStorage.instance.ref('uploads/${fileToDownload.name}');
task = ref.writeToFile(file);
emit(
  const FileState(
    status: BlocStatus.loaded,
  ),
);

}

 Future cancelSaving() async {
    if (task != null) {
      await task!.cancel();
    }
    emit(
      const FileState(
        status: BlocStatus.loaded,
      ),
    );
  }

Yes, it works. I added a new field to bloc:
late DownloadTask? task;
Initialize it while loading was in process in the save method and then use it in the cancel method.
Here is the methods:

 Future saveFile(FileModel fileToDownload) async {
emit(_loadingState());
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/${fileToDownload.name}');
final ref = FirebaseStorage.instance.ref('uploads/${fileToDownload.name}');
task = ref.writeToFile(file);
emit(
  const FileState(
    status: BlocStatus.loaded,
  ),
);

}

 Future cancelSaving() async {
    if (task != null) {
      await task!.cancel();
    }
    emit(
      const FileState(
        status: BlocStatus.loaded,
      ),
    );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文