tsacdop-podcast-app-android/lib/home/download_list.dart

137 lines
5.0 KiB
Dart
Raw Normal View History

2020-03-31 18:36:20 +02:00
import 'dart:io';
import 'package:flutter_downloader/flutter_downloader.dart';
2020-03-31 18:36:20 +02:00
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
2020-05-06 18:50:32 +02:00
import '../state/download_state.dart';
import '../episodes/episodedetail.dart';
import '../util/pageroute.dart';
2020-03-31 18:36:20 +02:00
class DownloadList extends StatefulWidget {
DownloadList({Key key}) : super(key: key);
@override
_DownloadListState createState() => _DownloadListState();
}
Widget _downloadButton(EpisodeTask task, BuildContext context) {
var downloader = Provider.of<DownloadState>(context, listen: false);
switch (task.status.value) {
case 2:
return IconButton(
icon: Icon(
Icons.pause_circle_filled,
),
onPressed: () => downloader.pauseTask(task.episode),
);
case 4:
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.refresh, color: Colors.red),
2020-03-31 18:36:20 +02:00
onPressed: () => downloader.retryTask(task.episode),
),
IconButton(
icon: Icon(Icons.close),
onPressed: () => downloader.delTask(task.episode),
),
],
);
case 6:
return IconButton(
icon: Icon(Icons.play_circle_filled),
onPressed: () => downloader.resumeTask(task.episode),
);
break;
default:
return Center();
}
}
class _DownloadListState extends State<DownloadList> {
@override
Widget build(BuildContext context) {
return SliverPadding(
2020-04-11 19:23:12 +02:00
padding: EdgeInsets.zero,
2020-03-31 18:36:20 +02:00
sliver: Consumer<DownloadState>(builder: (_, downloader, __) {
var tasks = downloader.episodeTasks
.where((task) => task.status.value != 3)
.toList();
return tasks.length > 0
2020-04-11 19:23:12 +02:00
? SliverPadding(
padding: EdgeInsets.all(5.0),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
onTap: () => Navigator.push(
context,
ScaleRoute(
page: EpisodeDetail(
episodeItem: tasks[index].episode,
)),
),
title: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 5,
child: Container(
child: Text(
tasks[index].episode.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
2020-03-31 18:36:20 +02:00
),
),
2020-04-11 19:23:12 +02:00
Expanded(
flex: 1,
child: tasks[index].progress >= 0 &&
tasks[index].status !=
DownloadTaskStatus.failed
2020-04-11 19:23:12 +02:00
? Container(
width: 40.0,
padding:
EdgeInsets.symmetric(horizontal: 2),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(6)),
color: Colors.red),
child: Text(
tasks[index].progress.toString() + '%',
2020-06-16 06:40:51 +02:00
textAlign: TextAlign.center,
maxLines: 1,
2020-04-11 19:23:12 +02:00
style: TextStyle(color: Colors.white),
))
: Container(),
),
],
),
subtitle: SizedBox(
height: 2,
child: LinearProgressIndicator(
value: tasks[index].progress / 100,
2020-03-31 18:36:20 +02:00
),
),
2020-04-11 19:23:12 +02:00
leading: CircleAvatar(
backgroundImage: FileImage(
File("${tasks[index].episode.imagePath}")),
),
2020-06-16 06:40:51 +02:00
trailing: _downloadButton(tasks[index], context),
2020-04-11 19:23:12 +02:00
);
},
childCount: tasks.length,
),
2020-03-31 18:36:20 +02:00
),
)
2020-04-11 19:23:12 +02:00
: SliverToBoxAdapter(
child: Center(),
2020-03-31 18:36:20 +02:00
);
}),
);
}
}