如何取消下载文件?
我有将文件下载到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它有效。我为集团添加了一个新领域:
延迟下载任务?任务;
在加载过程中,在保存方法中加载时初始化它,然后在取消方法中使用它。
这是方法:
}
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:
}