feat: ui improvement

flutter 3.7 compatibility
responsive view
beautiful progress bar
select file in treeview
downlading and downloaded page
This commit is contained in:
wuke2022 authored and liwei committed 2023-02-16 11:34:42 +08:00
1 parent a875a9258e
commit 9b1ed15fea
28 files changed
+932 -439

No files matched your search

+5
View File
@@ -5,6 +5,7 @@ import 'package:get/get.dart';
import '../../i18n/messages.dart';
import '../../routes/router.dart';
import '../../theme/theme.dart';
import '../home/home_controller.dart';
import 'app_controller.dart';
class AppView extends GetView<AppController> {
@@ -14,6 +15,10 @@ class AppView extends GetView<AppController> {
Widget build(BuildContext context) {
final config = controller.downloaderConfig.value;
return GetMaterialApp.router(
initialBinding: BindingsBuilder(() {
Get.lazyPut<HomeController>(() => HomeController(), fenix: true);
}),
useInheritedMediaQuery: true,
debugShowCheckedModeBanner: false,
theme: GopeedTheme.light,
darkTheme: GopeedTheme.dark,
@@ -1,3 +1,6 @@
import 'package:get/get.dart';
class CreateController extends GetxController {}
class CreateController extends GetxController {
final files = [].obs;
final RxList selectedIndexs = [].obs;
}
+14 -22
View File
@@ -20,6 +20,7 @@ class CreateView extends GetView<CreateController> {
final _urlController = TextEditingController();
final _confirmController = RoundedLoadingButtonController();
CreateView({Key? key}) : super(key: key);
@override
@@ -28,8 +29,8 @@ class CreateView extends GetView<CreateController> {
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Get.rootDelegate.offNamed(Routes.task)),
actions: [Container()],
onPressed: () => Get.rootDelegate.offNamed(Routes.downloading)),
// actions: [],
title: Text('create.title'.tr),
),
body: DropTarget(
@@ -47,9 +48,10 @@ class CreateView extends GetView<CreateController> {
autofocus: true,
controller: _urlController,
minLines: 1,
maxLines: 15,
maxLines: 30,
decoration: InputDecoration(
hintText: _hitText(),
hintStyle: const TextStyle(fontSize: 12),
labelText: 'create.downloadLink'.tr,
icon: const Icon(Icons.link)),
validator: (v) {
@@ -102,16 +104,15 @@ class CreateView extends GetView<CreateController> {
}
Future<void> _showResolveDialog(ResolveResult rr) async {
final res = rr.res;
controller.files.value = rr.res.files;
final appController = Get.find<AppController>();
final setting = appController.downloaderConfig.value;
final createFormKey = GlobalKey<FormState>();
final pathController = TextEditingController(text: setting.downloadDir);
final downloadController = RoundedLoadingButtonController();
var fileValues = List.filled(res.files.length, true);
return await showDialog<void>(
return showDialog<void>(
context: Get.context!,
barrierDismissible: false,
builder: (_) => AlertDialog(
@@ -129,11 +130,7 @@ class CreateView extends GetView<CreateController> {
autovalidateMode: AutovalidateMode.always,
child: Column(
children: [
Expanded(
child: FileListView(
files: res.files,
values: fileValues,
)),
Expanded(child: FileListView()),
DirectorySelector(
controller: pathController,
),
@@ -153,7 +150,7 @@ class CreateView extends GetView<CreateController> {
ElevatedButton.styleFrom(shape: const StadiumBorder())
.copyWith(
backgroundColor: MaterialStateProperty.all(
Get.theme.backgroundColor)),
Get.theme.colorScheme.background)),
onPressed: () {
Get.back();
},
@@ -183,17 +180,12 @@ class CreateView extends GetView<CreateController> {
await createTask(CreateTask(
rid: rr.id,
opts: Options(
name: '',
path: pathController.text,
selectFiles: fileValues
.asMap()
.entries
.where((e) => e.value)
.map((e) => e.key)
.toList(),
)));
name: '',
path: pathController.text,
selectFiles:
controller.selectedIndexs.cast<int>())));
Get.back();
Get.rootDelegate.offNamed(Routes.task);
Get.rootDelegate.offNamed(Routes.downloading);
}
} catch (e) {
Get.snackbar('error'.tr, e.toString());
@@ -0,0 +1,31 @@
import 'dart:async';
import 'package:get/get.dart';
import '../../api/api.dart';
import '../../api/model/task.dart';
class DownloadedController extends GetxController {
final tasks = <Task>[].obs;
late final Timer _timer;
@override
void onInit() async {
super.onInit();
await getTasksState();
_timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) async {
// debugPrint('timer${DateTime.now()}');
await getTasksState();
});
}
@override
void onClose() {
super.onClose();
_timer.cancel();
}
getTasksState() async {
tasks.value = await getTasks([Status.done]);
}
}
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gopeed/widget/buid_task_list_view.dart';
import 'downloaded_controller.dart';
class DownloadedView extends GetView<DownloadedController> {
const DownloadedView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BuildTaskListView(tasks: controller.tasks);
}
}
@@ -0,0 +1,33 @@
import 'dart:async';
import 'package:get/get.dart';
import '../../api/api.dart';
import '../../api/model/task.dart';
class DownloadingController extends GetxController {
final tasks = <Task>[].obs;
late final Timer _timer;
@override
void onInit() async {
super.onInit();
await getTasksState();
_timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) async {
// debugPrint('timer${DateTime.now()}');
await getTasksState();
});
}
@override
void onClose() {
super.onClose();
_timer.cancel();
}
getTasksState() async {
tasks.value = await getTasks(
[Status.ready, Status.running, Status.pause, Status.error]);
}
}
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../widget/buid_task_list_view.dart';
import 'downloading_controller.dart';
class DownloadingView extends GetView<DownloadingController> {
const DownloadingView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BuildTaskListView(tasks: controller.tasks);
}
}
+121 -66
View File
@@ -4,6 +4,7 @@ import 'package:stylish_bottom_bar/model/bar_items.dart';
import 'package:stylish_bottom_bar/stylish_bottom_bar.dart';
import '../../routes/router.dart';
import '../../util/responsive_builder.dart';
import 'home_controller.dart';
class HomeView extends GetView<HomeController> {
@@ -14,73 +15,127 @@ class HomeView extends GetView<HomeController> {
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(
option: AnimatedBarOptions(
iconSize: 32,
barAnimation: BarAnimation.fade,
iconStyle: IconStyle.Default,
opacity: 0.3,
),
items: [
BottomBarItem(
icon: const Icon(Icons.list),
selectedColor: Get.theme.primaryColor,
title: Text('home.task'.tr)),
BottomBarItem(
icon: const Icon(Icons.settings),
selectedColor: Get.theme.primaryColor,
title: Text('home.setting'.tr)),
],
hasNotch: true,
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,
extendBody: true,
body: Row(
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
!ResponsiveBuilder.isNarrow(context)
? NavigationRail(
extended: true,
labelType: NavigationRailLabelType.none,
minExtendedWidth: 140,
groupAlignment: 0,
// useIndicator: false,
onDestinationSelected: (int index) {
switch (index) {
case 0:
delegate.toNamed(Routes.downloading);
controller.currentIndex.value = 0;
break;
case 1:
delegate.toNamed(Routes.downloaded);
controller.currentIndex.value = 1;
break;
case 2:
delegate.toNamed(Routes.setting);
controller.currentIndex.value = 2;
break;
}
},
destinations: [
NavigationRailDestination(
icon: const Icon(Icons.format_align_left_outlined),
selectedIcon:
const Icon(Icons.format_align_left_outlined),
label: Text('home.downloading'.tr),
),
NavigationRailDestination(
icon:
const Icon(Icons.format_align_justify_outlined),
selectedIcon:
const Icon(Icons.format_align_justify_outlined),
label: Text('home.downloaded'.tr),
),
NavigationRailDestination(
icon: const Icon(Icons.settings),
selectedIcon: const Icon(Icons.settings),
label: Text('home.setting'.tr),
),
],
selectedIndex: controller.currentIndex.value,
leading: const Icon(Icons.logo_dev),
// trailing: const Icon(Icons.info_outline),
)
: const SizedBox.shrink(),
Expanded(
child: GetRouterOutlet(
initialRoute: Routes.downloading,
))
]),
bottomNavigationBar: ResponsiveBuilder.isNarrow(context)
? StylishBottomBar(
option: AnimatedBarOptions(
iconSize: 32,
barAnimation: BarAnimation.blink,
iconStyle: IconStyle.Default,
opacity: 0.3,
),
items: [
BottomBarItem(
icon: const Icon(Icons.format_align_left_outlined),
selectedColor: Get.theme.primaryColor,
title: Text('home.downloading'.tr)),
BottomBarItem(
icon: const Icon(Icons.format_align_justify_outlined),
selectedColor: Get.theme.primaryColor,
title: Text('home.downloaded'.tr)),
BottomBarItem(
icon: const Icon(Icons.settings),
selectedColor: Get.theme.primaryColor,
title: Text('home.setting'.tr)),
],
// hasNotch: true,
currentIndex: controller.currentIndex.value,
onTap: (index) {
switch (index) {
case 0:
delegate.toNamed(Routes.downloading);
controller.currentIndex.value = 0;
break;
case 1:
delegate.toNamed(Routes.downloaded);
controller.currentIndex.value = 1;
break;
case 2:
delegate.toNamed(Routes.setting);
controller.currentIndex.value = 2;
break;
}
},
)
: const SizedBox.shrink(),
// floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
// floatingActionButton: ResponsiveBuilder.isNarrow(context)
// ? 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,
// ))
// : null,
// floatingActionButtonLocation:
// FloatingActionButtonLocation.centerDocked,
);
},
);
+6 -8
View File
@@ -11,14 +11,12 @@ class RootView extends GetView<RootController> {
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);
// },
),
return GetRouterOutlet(
initialRoute: Routes.home,
// anchorRoute: '/',
// filterPages: (afterAnchor) {
// return afterAnchor.take(1);
// },
);
},
);
@@ -1,67 +0,0 @@
import 'dart:async';
import 'package:get/get.dart';
import '../../api/api.dart';
import '../../api/model/task.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;
loadTab() async {
if (tabIndex.value == 0) {
if (await _loadUnDone()) {
await _loadDone();
}
} else {
if (await _loadDone()) {
await _loadUnDone();
}
}
}
@override
void onClose() {
super.onClose();
_timer.cancel();
}
@override
void onInit() {
super.onInit();
loadTab();
_timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) async {
loadTab();
});
}
_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;
}
_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;
}
}
-198
View File
@@ -1,198 +0,0 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../api/api.dart';
import '../../api/model/task.dart';
import '../../util/util.dart';
import 'task_controller.dart';
class Item extends StatelessWidget {
final Task task;
const Item({Key? key, required this.task}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
elevation: 4.0,
child: InkWell(
onTap: () {},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Text(task.meta.res.name),
leading: const Icon(Icons.insert_drive_file),
),
Row(
children: [
SizedBox(
width: 180,
child: Text(
"${isDone() ? "" : "${Util.fmtByte(task.progress.downloaded)} / "}${Util.fmtByte(task.size)}",
style: context.textTheme.bodyText1
?.copyWith(color: Get.theme.disabledColor),
).padding(left: 18)),
Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text("${Util.fmtByte(task.progress.speed)} / s",
style: context.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 {
final file = File(Util.buildAbsPath(task.meta.opts.path,
task.meta.res.files[0].path, task.meta.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;
}
double getProgress() {
return task.size <= 0 ? 1 : task.progress.downloaded / task.size;
}
bool isDone() {
return task.status == Status.done;
}
bool isRunning() {
return task.status == Status.running;
}
Future<void> _showDeleteDialog(String id) {
final keep = true.obs;
final context = Get.context!;
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => AlertDialog(
title: Text('task.deleteTask'.tr),
content: Obx(() => CheckboxListTile(
value: keep.value,
title: Text('task.deleteTaskTip'.tr,
style: context.textTheme.bodyText1),
onChanged: (v) {
keep.value = v!;
})),
actions: [
TextButton(
child: Text('cancel'.tr),
onPressed: () => Get.back(),
),
TextButton(
child: Text(
'task.delete'.tr,
style: const TextStyle(color: Colors.redAccent),
),
onPressed: () async {
try {
await deleteTask(id, !keep.value);
Get.find<TaskController>().loadTab();
Get.back();
} catch (e) {
Get.snackbar('error'.tr, e.toString());
}
},
),
],
));
}
}
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) {
if (controller.tabIndex.value != index) {
controller.tabIndex.value = index;
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]);
},
);
}
}