mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-17 13:53:41 +00:00
feat: flutter ui integration
This commit is contained in:
1 parent
0fc133e46d
commit
c4a30dc4c2
170 files changed
+7783
-1
No files matched your search
@@ -0,0 +1,5 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class CreateController extends GetxController {
|
||||
var currentIndex = 0.obs;
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
import 'dart:developer' as developer;
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gopeed/api/api.dart';
|
||||
import 'package:gopeed/api/model/create_task.dart';
|
||||
import 'package:gopeed/api/model/options.dart';
|
||||
import 'package:gopeed/api/model/request.dart';
|
||||
import 'package:gopeed/api/model/resource.dart';
|
||||
import 'package:gopeed/pages/create/create_controller.dart';
|
||||
import 'package:gopeed/setting/setting.dart';
|
||||
import 'package:gopeed/widget/file_list_view.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:rounded_loading_button/rounded_loading_button.dart';
|
||||
|
||||
import '../../routes/router.dart';
|
||||
import '../../widget/directory_selector.dart';
|
||||
|
||||
class CreateView extends GetView<CreateController> {
|
||||
CreateView({Key? key}) : super(key: key);
|
||||
|
||||
final _resolveFormKey = GlobalKey<FormState>();
|
||||
final _urlController = TextEditingController(
|
||||
text: "https://dldir1.qq.com/weixin/Windows/WeChatSetup.exe");
|
||||
final _confirmController = RoundedLoadingButtonController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => Get.rootDelegate.offNamed(Routes.task)),
|
||||
actions: [Container()],
|
||||
title: const Text("创建任务"),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
|
||||
child: Form(
|
||||
key: _resolveFormKey,
|
||||
autovalidateMode: AutovalidateMode.always, //开启自动校验
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
autofocus: true,
|
||||
controller: _urlController,
|
||||
minLines: 1,
|
||||
maxLines: 15,
|
||||
decoration: const InputDecoration(
|
||||
hintText: "请输入下载链接,支持 HTTP/HTTPS/MAGNET",
|
||||
labelText: "下载链接",
|
||||
icon: Icon(Icons.link)),
|
||||
validator: (v) {
|
||||
return v!.trim().isNotEmpty ? null : "请输入下载链接";
|
||||
}),
|
||||
// 登录按钮
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(15.0),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints.tightFor(
|
||||
width: 150,
|
||||
height: 40,
|
||||
),
|
||||
child: RoundedLoadingButton(
|
||||
color: Get.theme.colorScheme.secondary,
|
||||
onPressed: () async {
|
||||
if (_resolveFormKey.currentState!.validate()) {
|
||||
_confirmController.start();
|
||||
try {
|
||||
final res = await resolve(Request(
|
||||
url: _urlController.text,
|
||||
));
|
||||
await _showResolveDialog(res);
|
||||
} catch (e) {
|
||||
Get.snackbar("错误", e.toString());
|
||||
} finally {
|
||||
_confirmController.reset();
|
||||
}
|
||||
}
|
||||
},
|
||||
controller: _confirmController,
|
||||
child: const Text("确认"),
|
||||
),
|
||||
)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showResolveDialog(Resource res) async {
|
||||
final createFormKey = GlobalKey<FormState>();
|
||||
final pathController =
|
||||
TextEditingController(text: Setting.instance.downloadDir);
|
||||
final downloadController = RoundedLoadingButtonController();
|
||||
var fileValues = List.filled(res.files.length, true);
|
||||
|
||||
return await showDialog<void>(
|
||||
context: Get.context!,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => AlertDialog(
|
||||
content: Builder(
|
||||
builder: (context) {
|
||||
// Get available height and width of the build area of this widget. Make a choice depending on the size.
|
||||
var height = MediaQuery.of(context).size.height;
|
||||
var width = MediaQuery.of(context).size.width;
|
||||
|
||||
return SizedBox(
|
||||
height: height * 0.5,
|
||||
width: width,
|
||||
child: Form(
|
||||
key: createFormKey,
|
||||
autovalidateMode: AutovalidateMode.always, //开启自动校验
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FileListView(
|
||||
files: res.files,
|
||||
values: fileValues,
|
||||
)),
|
||||
DirectorySelector(
|
||||
controller: pathController,
|
||||
),
|
||||
],
|
||||
)),
|
||||
);
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints.tightFor(
|
||||
width: Get.theme.buttonTheme.minWidth,
|
||||
height: Get.theme.buttonTheme.height,
|
||||
),
|
||||
child: ElevatedButton(
|
||||
style:
|
||||
ElevatedButton.styleFrom(shape: const StadiumBorder())
|
||||
.copyWith(
|
||||
backgroundColor: MaterialStateProperty.all(
|
||||
Get.theme.backgroundColor)),
|
||||
onPressed: () {
|
||||
Get.back();
|
||||
},
|
||||
child: const Text("取消"),
|
||||
),
|
||||
),
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints.tightFor(
|
||||
width: Get.theme.buttonTheme.minWidth,
|
||||
height: Get.theme.buttonTheme.height,
|
||||
),
|
||||
child: RoundedLoadingButton(
|
||||
color: Get.theme.colorScheme.secondary,
|
||||
onPressed: () async {
|
||||
try {
|
||||
if (createFormKey.currentState!.validate()) {
|
||||
downloadController.start();
|
||||
if (Platform.isAndroid) {
|
||||
if (!await Permission.storage.request().isGranted) {
|
||||
Get.snackbar("错误", "需要开启存储权限");
|
||||
return;
|
||||
}
|
||||
}
|
||||
await createTask(CreateTask(
|
||||
res: res,
|
||||
opts: Options(
|
||||
name: '',
|
||||
path: pathController.text,
|
||||
connections: Setting.instance.connections,
|
||||
selectFiles: fileValues
|
||||
.asMap()
|
||||
.entries
|
||||
.where((e) => e.value)
|
||||
.map((e) => e.key)
|
||||
.toList(),
|
||||
)));
|
||||
Get.back();
|
||||
Get.rootDelegate.offNamed(Routes.task);
|
||||
}
|
||||
} catch (e) {
|
||||
Get.snackbar("错误", e.toString());
|
||||
developer.log(e.toString(), name: 'flutter');
|
||||
} finally {
|
||||
downloadController.reset();
|
||||
}
|
||||
},
|
||||
controller: downloadController,
|
||||
child: const Text("下载"),
|
||||
),
|
||||
),
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class HomeController extends GetxController {
|
||||
var currentIndex = 0.obs;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:stylish_bottom_bar/stylish_bottom_bar.dart';
|
||||
|
||||
import '../../routes/router.dart';
|
||||
import 'home_controller.dart';
|
||||
|
||||
class HomeView extends GetView<HomeController> {
|
||||
const HomeView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GetRouterOutlet.builder(
|
||||
builder: (context, delegate, currentRoute) {
|
||||
//This router outlet handles the appbar and the bottom navigation bar
|
||||
final currentLocation = currentRoute?.location;
|
||||
var currentIndex = 0;
|
||||
if (currentLocation?.startsWith(Routes.setting) == true) {
|
||||
currentIndex = 1;
|
||||
}
|
||||
return Scaffold(
|
||||
extendBody: true, //to make floating action button notch transparent
|
||||
|
||||
//to avoid the floating action button overlapping behavior,
|
||||
// when a soft keyboard is displayed
|
||||
// resizeToAvoidBottomInset: false,
|
||||
body: GetRouterOutlet(
|
||||
initialRoute: Routes.task,
|
||||
|
||||
//delegate: Get.nestedKey(Routes.HOME),
|
||||
// key: Get.nestedKey(Routes.HOME),
|
||||
// key: Get.nestedKey(Routes.home),
|
||||
),
|
||||
|
||||
bottomNavigationBar: StylishBottomBar(
|
||||
items: [
|
||||
AnimatedBarItems(
|
||||
icon: const Icon(Icons.list),
|
||||
selectedColor: Get.theme.primaryColor,
|
||||
title: const Text('任务')),
|
||||
AnimatedBarItems(
|
||||
icon: const Icon(Icons.settings),
|
||||
selectedColor: Get.theme.primaryColor,
|
||||
title: const Text('设置')),
|
||||
],
|
||||
iconSize: 32,
|
||||
barAnimation: BarAnimation.fade,
|
||||
iconStyle: IconStyle.Default,
|
||||
hasNotch: true,
|
||||
opacity: 0.3,
|
||||
currentIndex: currentIndex,
|
||||
onTap: (index) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
delegate.toNamed(Routes.task);
|
||||
break;
|
||||
case 1:
|
||||
delegate.toNamed(Routes.setting);
|
||||
break;
|
||||
}
|
||||
},
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
/* Get.isDarkMode
|
||||
? {
|
||||
Get.changeTheme(GopeedTheme.light),
|
||||
Get.changeThemeMode(ThemeMode.light)
|
||||
}
|
||||
: {
|
||||
Get.changeTheme(GopeedTheme.dark),
|
||||
Get.changeThemeMode(ThemeMode.dark)
|
||||
}; */
|
||||
Get.rootDelegate.toNamed(Routes.create);
|
||||
},
|
||||
child: const Icon(
|
||||
Icons.add,
|
||||
)),
|
||||
floatingActionButtonLocation:
|
||||
FloatingActionButtonLocation.centerDocked,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class RootController extends GetxController {
|
||||
//TODO: Implement RootController
|
||||
|
||||
final count = 0.obs;
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {
|
||||
super.onReady();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {}
|
||||
void increment() => count.value++;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../../routes/router.dart';
|
||||
import 'root_controller.dart';
|
||||
|
||||
class RootView extends GetView<RootController> {
|
||||
const RootView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GetRouterOutlet.builder(
|
||||
builder: (context, delegate, current) {
|
||||
return Scaffold(
|
||||
body: GetRouterOutlet(
|
||||
initialRoute: Routes.home,
|
||||
// anchorRoute: '/',
|
||||
// filterPages: (afterAnchor) {
|
||||
// return afterAnchor.take(1);
|
||||
// },
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gopeed/api/api.dart';
|
||||
import 'package:gopeed/api/model/server_config.dart';
|
||||
|
||||
import '../../setting/setting.dart';
|
||||
|
||||
class SettingController extends GetxController {
|
||||
final setting = Setting.instance.obs;
|
||||
final tapStatues = <int, bool>{}.obs;
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gopeed/setting/setting.dart';
|
||||
import 'package:gopeed/widget/directory_selector.dart';
|
||||
|
||||
import 'setting_controller.dart';
|
||||
|
||||
class SettingView extends GetView<SettingController> {
|
||||
const SettingView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Timer? timer;
|
||||
debounceSave() {
|
||||
timer?.cancel();
|
||||
timer = Timer(const Duration(milliseconds: 500), Setting.instance.save);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('设置'),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: Column(
|
||||
// 左对齐
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text("基本设置", style: Get.textTheme.titleLarge),
|
||||
const SizedBox(height: 10),
|
||||
Obx(() => Card(
|
||||
child: Column(
|
||||
children: _buildConfigItems([
|
||||
_ConfigItem(
|
||||
"主题",
|
||||
() => _getThemeName(controller.setting.value.themeMode),
|
||||
() => DropdownButton<ThemeMode>(
|
||||
value: controller.setting.value.themeMode,
|
||||
isDense: true,
|
||||
onChanged: (value) {
|
||||
controller.setting.value.themeMode = value!;
|
||||
Get.changeThemeMode(value);
|
||||
|
||||
debounceSave();
|
||||
},
|
||||
items: ThemeMode.values
|
||||
.map((e) => DropdownMenuItem<ThemeMode>(
|
||||
value: e,
|
||||
child: Text(_getThemeName(e)),
|
||||
))
|
||||
.toList(),
|
||||
)),
|
||||
_ConfigItem(
|
||||
"下载目录", () => controller.setting.value.downloadDir, () {
|
||||
final downloadDirController = TextEditingController(
|
||||
text: controller.setting.value.downloadDir);
|
||||
downloadDirController.addListener(() {
|
||||
if (downloadDirController.text !=
|
||||
controller.setting.value.downloadDir) {
|
||||
controller.setting.value.downloadDir =
|
||||
downloadDirController.text;
|
||||
|
||||
debounceSave();
|
||||
}
|
||||
});
|
||||
return DirectorySelector(
|
||||
controller: downloadDirController,
|
||||
showLabel: false,
|
||||
);
|
||||
}),
|
||||
_ConfigItem("连接数",
|
||||
() => controller.setting.value.connections.toString(),
|
||||
() {
|
||||
final connectionsController = TextEditingController(
|
||||
text: controller.setting.value.connections.toString());
|
||||
connectionsController.addListener(() {
|
||||
if (connectionsController.text.isNotEmpty &&
|
||||
connectionsController.text !=
|
||||
controller.setting.value.connections.toString()) {
|
||||
controller.setting.value.connections =
|
||||
int.parse(connectionsController.text);
|
||||
|
||||
debounceSave();
|
||||
}
|
||||
});
|
||||
|
||||
return TextField(
|
||||
controller: connectionsController,
|
||||
keyboardType: TextInputType.number,
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.digitsOnly,
|
||||
NumericalRangeFormatter(min: 1, max: 256),
|
||||
],
|
||||
);
|
||||
}),
|
||||
]),
|
||||
))),
|
||||
],
|
||||
).paddingAll(16),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildConfigItems(List<_ConfigItem> buildItems) {
|
||||
final result = <Widget>[];
|
||||
for (var i = 0; i < buildItems.length; i++) {
|
||||
final buildItem = buildItems[i];
|
||||
result.add(ListTile(
|
||||
title: Text(buildItem.label),
|
||||
subtitle: controller.tapStatues[i] ?? false
|
||||
? buildItem.inputItem()
|
||||
: Text(buildItem.text()),
|
||||
onTap: () {
|
||||
controller.tapStatues[i] = true;
|
||||
// set other false
|
||||
for (var j = 0; j < controller.tapStatues.length; j++) {
|
||||
if (j != i) {
|
||||
controller.tapStatues[j] = false;
|
||||
}
|
||||
}
|
||||
}));
|
||||
if (i != buildItems.length - 1) {
|
||||
result.add(const Divider());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
String _getThemeName(ThemeMode themeMode) {
|
||||
switch (themeMode) {
|
||||
case ThemeMode.system:
|
||||
return "跟随系统";
|
||||
case ThemeMode.light:
|
||||
return "明亮主题";
|
||||
case ThemeMode.dark:
|
||||
return "暗黑主题";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _ConfigItem {
|
||||
late String label;
|
||||
late String Function() text;
|
||||
late Widget Function() inputItem;
|
||||
|
||||
_ConfigItem(this.label, this.text, this.inputItem);
|
||||
}
|
||||
|
||||
class NumericalRangeFormatter extends TextInputFormatter {
|
||||
final int min;
|
||||
final int max;
|
||||
|
||||
NumericalRangeFormatter({required this.min, required this.max});
|
||||
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(
|
||||
TextEditingValue oldValue,
|
||||
TextEditingValue newValue,
|
||||
) {
|
||||
if (newValue.text.isEmpty) {
|
||||
return newValue;
|
||||
}
|
||||
var intVal = int.tryParse(newValue.text);
|
||||
if (intVal == null) {
|
||||
return oldValue;
|
||||
}
|
||||
if (intVal < min) {
|
||||
return newValue.copyWith(text: min.toString());
|
||||
} else if (intVal > max) {
|
||||
return oldValue.copyWith(text: max.toString());
|
||||
} else {
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gopeed/api/model/task.dart';
|
||||
import 'package:gopeed/core/libgopeed_boot.dart';
|
||||
|
||||
import '../../api/api.dart';
|
||||
|
||||
class TaskController extends GetxController {
|
||||
final tabIndex = 0.obs;
|
||||
final unDoneTasks = <Task>[].obs;
|
||||
final doneTasks = <Task>[].obs;
|
||||
|
||||
var _lastUnDoneSize = 0;
|
||||
var _lastDoneSize = 0;
|
||||
|
||||
late final Timer _timer;
|
||||
|
||||
_loadUnDone() async {
|
||||
unDoneTasks.value = await getTasks(
|
||||
[Status.ready, Status.running, Status.pause, Status.error]);
|
||||
final lastLastSize = _lastUnDoneSize;
|
||||
if (lastLastSize != unDoneTasks.length) {
|
||||
_lastUnDoneSize = unDoneTasks.length;
|
||||
}
|
||||
// when unDoneTasks.length reduce, it means some task is done, refresh doneTasks
|
||||
return lastLastSize > _lastUnDoneSize;
|
||||
}
|
||||
|
||||
_loadDone() async {
|
||||
doneTasks.value = await getTasks([Status.done]);
|
||||
final lastLastSize = _lastDoneSize;
|
||||
if (lastLastSize != doneTasks.length) {
|
||||
_lastDoneSize = doneTasks.length;
|
||||
}
|
||||
// when doneTasks.length increase, it means some task is done, refresh unDoneTasks
|
||||
return lastLastSize < _lastDoneSize;
|
||||
}
|
||||
|
||||
loadTab() async {
|
||||
if (tabIndex.value == 0) {
|
||||
if (await _loadUnDone()) {
|
||||
await _loadDone();
|
||||
}
|
||||
} else {
|
||||
if (await _loadDone()) {
|
||||
await _loadUnDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
|
||||
loadTab();
|
||||
_timer = Timer.periodic(
|
||||
Duration(milliseconds: LibgopeedBoot.instance.refreshInterval),
|
||||
(timer) async {
|
||||
loadTab();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
super.onClose();
|
||||
|
||||
_timer.cancel();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gopeed/api/api.dart';
|
||||
import 'package:gopeed/api/model/task.dart';
|
||||
import 'package:styled_widget/styled_widget.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../../util/util.dart';
|
||||
import 'task_controller.dart';
|
||||
|
||||
class TaskView extends GetView<TaskController> {
|
||||
const TaskView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DefaultTabController(
|
||||
length: 2,
|
||||
child: Scaffold(
|
||||
appBar: PreferredSize(
|
||||
preferredSize: const Size.fromHeight(56),
|
||||
child: AppBar(
|
||||
bottom: TabBar(
|
||||
tabs: const [
|
||||
Tab(
|
||||
icon: Icon(Icons.file_download),
|
||||
),
|
||||
Tab(
|
||||
icon: Icon(Icons.done),
|
||||
),
|
||||
],
|
||||
onTap: (index) {
|
||||
controller.tabIndex.value = index;
|
||||
if (index == 1) {
|
||||
controller.loadTab();
|
||||
}
|
||||
},
|
||||
),
|
||||
)),
|
||||
body: TabBarView(
|
||||
children: [
|
||||
Obx(() => buildTaskList(controller.unDoneTasks)),
|
||||
Obx(() => buildTaskList(controller.doneTasks)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildTaskList(List<Task> tasks) {
|
||||
return ListView.builder(
|
||||
itemCount: tasks.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Item(task: tasks[index]);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Item extends StatelessWidget {
|
||||
final Task task;
|
||||
|
||||
const Item({Key? key, required this.task}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var controller = Get.find<TaskController>();
|
||||
|
||||
return Card(
|
||||
elevation: 4.0,
|
||||
child: InkWell(
|
||||
onTap: () {},
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(task.res.name),
|
||||
leading: const Icon(Icons.insert_drive_file),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Text(
|
||||
"${isDone() ? "" : "${Util.fmtByte(task.progress.downloaded)} / "}${Util.fmtByte(task.size)}",
|
||||
style: Get.textTheme.bodyText1
|
||||
?.copyWith(color: Get.theme.disabledColor),
|
||||
).padding(left: 18)),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Text("${Util.fmtByte(task.progress.speed)} / s",
|
||||
style: Get.textTheme.subtitle2),
|
||||
...buildActions()
|
||||
],
|
||||
)),
|
||||
],
|
||||
),
|
||||
LinearProgressIndicator(
|
||||
value: getProgress(),
|
||||
),
|
||||
],
|
||||
),
|
||||
)).padding(horizontal: 8, top: 8);
|
||||
}
|
||||
|
||||
List<Widget> buildActions() {
|
||||
final list = <Widget>[];
|
||||
if (isDone()) {
|
||||
if (Util.isDesktop()) {
|
||||
list.add(IconButton(
|
||||
icon: const Icon(Icons.folder_open),
|
||||
onPressed: () async {
|
||||
print(Util.buildAbsPath(task.opts.path, task.res.files[0].path,
|
||||
task.res.files[0].name));
|
||||
final file = File(Util.buildAbsPath(task.opts.path,
|
||||
task.res.files[0].path, task.res.files[0].name));
|
||||
await launchUrl(file.parent.uri);
|
||||
},
|
||||
));
|
||||
}
|
||||
} else {
|
||||
if (isRunning()) {
|
||||
list.add(IconButton(
|
||||
icon: const Icon(Icons.pause),
|
||||
onPressed: () async {
|
||||
await pauseTask(task.id);
|
||||
},
|
||||
));
|
||||
} else {
|
||||
list.add(IconButton(
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
onPressed: () async {
|
||||
await continueTask(task.id);
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
list.add(IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: () {
|
||||
_showDeleteDialog(task.id);
|
||||
},
|
||||
));
|
||||
return list;
|
||||
}
|
||||
|
||||
Future<void> _showDeleteDialog(String id) {
|
||||
final keep = true.obs;
|
||||
|
||||
return showDialog<void>(
|
||||
context: Get.context!,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text("删除任务"),
|
||||
content: Obx(() => CheckboxListTile(
|
||||
value: keep.value,
|
||||
title: Text("保留已下载的文件", style: Get.textTheme.bodyText1),
|
||||
onChanged: (v) {
|
||||
keep.value = v!;
|
||||
})),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text("取消"),
|
||||
onPressed: () => Get.back(), //关闭对话框
|
||||
),
|
||||
TextButton(
|
||||
child: const Text(
|
||||
"删除",
|
||||
style: TextStyle(color: Colors.redAccent),
|
||||
),
|
||||
onPressed: () async {
|
||||
try {
|
||||
await deleteTask(id, !keep.value);
|
||||
Get.find<TaskController>().loadTab();
|
||||
Get.back();
|
||||
} catch (e) {
|
||||
Get.snackbar("错误", e.toString());
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
double getProgress() {
|
||||
return task.size <= 0 ? 1 : task.progress.downloaded / task.size;
|
||||
}
|
||||
|
||||
bool isRunning() {
|
||||
return task.status == Status.running;
|
||||
}
|
||||
|
||||
bool isDone() {
|
||||
return task.status == Status.done;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user