mirror of
https://github.com/qinglong-app/qinglong_app.git
synced 2025-10-09 16:48:19 +08:00
支持上传脚本
This commit is contained in:
154
lib/module/others/scripts/script_code_detail_page.dart
Normal file
154
lib/module/others/scripts/script_code_detail_page.dart
Normal file
@@ -0,0 +1,154 @@
|
||||
import 'package:code_text_field/code_text_field.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:highlight/languages/javascript.dart';
|
||||
import 'package:highlight/languages/json.dart';
|
||||
import 'package:highlight/languages/powershell.dart';
|
||||
import 'package:highlight/languages/python.dart';
|
||||
import 'package:highlight/languages/vbscript-html.dart';
|
||||
import 'package:highlight/languages/yaml.dart';
|
||||
import 'package:qinglong_app/base/ql_app_bar.dart';
|
||||
import 'package:qinglong_app/base/sp_const.dart';
|
||||
import 'package:qinglong_app/base/theme.dart';
|
||||
import 'package:qinglong_app/utils/sp_utils.dart';
|
||||
|
||||
/// @author NewTab
|
||||
class ScriptCodeDetailPage extends ConsumerStatefulWidget {
|
||||
final String title;
|
||||
final String content;
|
||||
|
||||
const ScriptCodeDetailPage({
|
||||
Key? key,
|
||||
required this.title,
|
||||
required this.content,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
ScriptCodeDetailPageState createState() => ScriptCodeDetailPageState();
|
||||
}
|
||||
|
||||
class ScriptCodeDetailPageState extends ConsumerState<ScriptCodeDetailPage> {
|
||||
CodeController? _codeController;
|
||||
GlobalKey<CodeFieldState> codeFieldKey = GlobalKey();
|
||||
|
||||
bool buttonshow = false;
|
||||
|
||||
void scrollToTop() {
|
||||
codeFieldKey.currentState?.getCodeScroll()?.animateTo(0,
|
||||
duration: const Duration(milliseconds: 200), curve: Curves.linear);
|
||||
}
|
||||
|
||||
void floatingButtonVisibility() {
|
||||
double y = codeFieldKey.currentState?.getCodeScroll()?.offset ?? 0;
|
||||
if (y > MediaQuery.of(context).size.height / 2) {
|
||||
if (buttonshow == true) return;
|
||||
setState(() {
|
||||
buttonshow = true;
|
||||
});
|
||||
} else {
|
||||
if (buttonshow == false) return;
|
||||
setState(() {
|
||||
buttonshow = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String suffix = "\n\n\n";
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_codeController?.dispose();
|
||||
_codeController = null;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
getLanguageType(String title) {
|
||||
if (title.endsWith(".js")) {
|
||||
return javascript;
|
||||
}
|
||||
|
||||
if (title.endsWith(".sh")) {
|
||||
return powershell;
|
||||
}
|
||||
|
||||
if (title.endsWith(".py")) {
|
||||
return python;
|
||||
}
|
||||
if (title.endsWith(".json")) {
|
||||
return json;
|
||||
}
|
||||
if (title.endsWith(".yaml")) {
|
||||
return yaml;
|
||||
}
|
||||
return vbscriptHtml;
|
||||
}
|
||||
|
||||
late String content;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
content = widget.content;
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_codeController ??= CodeController(
|
||||
text: (content) + suffix,
|
||||
language: getLanguageType(widget.title),
|
||||
onChange: (value) {
|
||||
content = value + suffix;
|
||||
},
|
||||
theme: ref.watch(themeProvider).themeColor.codeEditorTheme(),
|
||||
stringMap: {
|
||||
"export": const TextStyle(
|
||||
fontWeight: FontWeight.normal, color: Color(0xff6B2375)),
|
||||
},
|
||||
);
|
||||
return Scaffold(
|
||||
floatingActionButton: Visibility(
|
||||
visible: buttonshow,
|
||||
child: FloatingActionButton(
|
||||
mini: true,
|
||||
onPressed: () {
|
||||
scrollToTop();
|
||||
},
|
||||
elevation: 2,
|
||||
backgroundColor: Colors.white,
|
||||
child: const Icon(CupertinoIcons.up_arrow),
|
||||
),
|
||||
),
|
||||
appBar: QlAppBar(
|
||||
canBack: true,
|
||||
backCall: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
title: widget.title,
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: SpUtil.getBool(spShowLine, defValue: false) ? 0 : 10,
|
||||
),
|
||||
child: CodeField(
|
||||
key: codeFieldKey,
|
||||
controller: _codeController!,
|
||||
expands: true,
|
||||
readOnly: true,
|
||||
wrap: SpUtil.getBool(spShowLine, defValue: false) ? false : true,
|
||||
hideColumn: !SpUtil.getBool(spShowLine, defValue: false),
|
||||
lineNumberStyle: LineNumberStyle(
|
||||
textStyle: TextStyle(
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
background: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,8 @@ class ScriptDetailPage extends ConsumerStatefulWidget {
|
||||
_ScriptDetailPageState createState() => _ScriptDetailPageState();
|
||||
}
|
||||
|
||||
class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLoadState<ScriptDetailPage> {
|
||||
class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage>
|
||||
with LazyLoadState<ScriptDetailPage> {
|
||||
String? content;
|
||||
CodeController? _codeController;
|
||||
GlobalKey<CodeFieldState> codeFieldKey = GlobalKey();
|
||||
@@ -42,7 +43,8 @@ class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLo
|
||||
bool buttonshow = false;
|
||||
|
||||
void scrollToTop() {
|
||||
codeFieldKey.currentState?.getCodeScroll()?.animateTo(0, duration: const Duration(milliseconds: 200), curve: Curves.linear);
|
||||
codeFieldKey.currentState?.getCodeScroll()?.animateTo(0,
|
||||
duration: const Duration(milliseconds: 200), curve: Curves.linear);
|
||||
}
|
||||
|
||||
void floatingButtonVisibility() {
|
||||
@@ -164,7 +166,8 @@ class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLo
|
||||
),
|
||||
onPressed: () async {
|
||||
Navigator.of(context).pop();
|
||||
HttpResponse<NullResponse> result = await Api.delScript(widget.title, widget.path ?? "");
|
||||
HttpResponse<NullResponse> result =
|
||||
await Api.delScript(widget.title, widget.path ?? "");
|
||||
if (result.success) {
|
||||
"删除成功".toast();
|
||||
Navigator.of(context).pop(true);
|
||||
@@ -209,7 +212,8 @@ class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLo
|
||||
},
|
||||
theme: ref.watch(themeProvider).themeColor.codeEditorTheme(),
|
||||
stringMap: {
|
||||
"export": const TextStyle(fontWeight: FontWeight.normal, color: Color(0xff6B2375)),
|
||||
"export": const TextStyle(
|
||||
fontWeight: FontWeight.normal, color: Color(0xff6B2375)),
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -296,31 +300,34 @@ class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLo
|
||||
),
|
||||
body: content == null
|
||||
? const Center(
|
||||
child: CupertinoActivityIndicator(),
|
||||
)
|
||||
child: CupertinoActivityIndicator(),
|
||||
)
|
||||
: SafeArea(
|
||||
top: false,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: SpUtil.getBool(spShowLine, defValue: false) ? 0 : 10,
|
||||
),
|
||||
child: CodeField(
|
||||
key: codeFieldKey,
|
||||
controller: _codeController!,
|
||||
expands: true,
|
||||
readOnly: true,
|
||||
wrap: SpUtil.getBool(spShowLine, defValue: false) ? false : true,
|
||||
hideColumn: !SpUtil.getBool(spShowLine, defValue: false),
|
||||
lineNumberStyle: LineNumberStyle(
|
||||
textStyle: TextStyle(
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
fontSize: 12,
|
||||
top: false,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal:
|
||||
SpUtil.getBool(spShowLine, defValue: false) ? 0 : 10,
|
||||
),
|
||||
child: CodeField(
|
||||
key: codeFieldKey,
|
||||
controller: _codeController!,
|
||||
expands: true,
|
||||
readOnly: true,
|
||||
wrap: SpUtil.getBool(spShowLine, defValue: false)
|
||||
? false
|
||||
: true,
|
||||
hideColumn: !SpUtil.getBool(spShowLine, defValue: false),
|
||||
lineNumberStyle: LineNumberStyle(
|
||||
textStyle: TextStyle(
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
background: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
background: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -337,8 +344,10 @@ class _ScriptDetailPageState extends ConsumerState<ScriptDetailPage> with LazyLo
|
||||
const Duration(
|
||||
seconds: 1,
|
||||
),
|
||||
() {
|
||||
codeFieldKey.currentState?.getCodeScroll()?.addListener(floatingButtonVisibility);
|
||||
() {
|
||||
codeFieldKey.currentState
|
||||
?.getCodeScroll()
|
||||
?.addListener(floatingButtonVisibility);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
|
||||
@@ -21,7 +21,8 @@ class ScriptEditPage extends ConsumerStatefulWidget {
|
||||
final String title;
|
||||
final String path;
|
||||
|
||||
const ScriptEditPage(this.title, this.path, this.content, {Key? key}) : super(key: key);
|
||||
const ScriptEditPage(this.title, this.path, this.content, {Key? key})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
_ScriptEditPageState createState() => _ScriptEditPageState();
|
||||
@@ -81,7 +82,8 @@ class _ScriptEditPageState extends ConsumerState<ScriptEditPage> {
|
||||
},
|
||||
theme: ref.watch(themeProvider).themeColor.codeEditorTheme(),
|
||||
stringMap: {
|
||||
"export": const TextStyle(fontWeight: FontWeight.normal, color: Color(0xff6B2375)),
|
||||
"export": const TextStyle(
|
||||
fontWeight: FontWeight.normal, color: Color(0xff6B2375)),
|
||||
},
|
||||
);
|
||||
return Scaffold(
|
||||
@@ -132,7 +134,8 @@ class _ScriptEditPageState extends ConsumerState<ScriptEditPage> {
|
||||
actions: [
|
||||
InkWell(
|
||||
onTap: () async {
|
||||
HttpResponse<NullResponse> response = await Api.updateScript(widget.title, widget.path, result);
|
||||
HttpResponse<NullResponse> response =
|
||||
await Api.updateScript(widget.title, widget.path, result);
|
||||
if (response.success) {
|
||||
"提交成功".toast();
|
||||
Navigator.of(context).pop(true);
|
||||
|
||||
@@ -11,6 +11,8 @@ import 'package:qinglong_app/base/ui/search_cell.dart';
|
||||
import 'package:qinglong_app/module/others/scripts/script_bean.dart';
|
||||
import 'package:qinglong_app/utils/extension.dart';
|
||||
|
||||
import 'script_upload_page.dart';
|
||||
|
||||
/// @author NewTab
|
||||
class ScriptPage extends ConsumerStatefulWidget {
|
||||
const ScriptPage({Key? key}) : super(key: key);
|
||||
@@ -19,7 +21,8 @@ class ScriptPage extends ConsumerStatefulWidget {
|
||||
_ScriptPageState createState() => _ScriptPageState();
|
||||
}
|
||||
|
||||
class _ScriptPageState extends ConsumerState<ScriptPage> with LazyLoadState<ScriptPage> {
|
||||
class _ScriptPageState extends ConsumerState<ScriptPage>
|
||||
with LazyLoadState<ScriptPage> {
|
||||
List<ScriptBean> list = [];
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
|
||||
@@ -75,7 +78,8 @@ class _ScriptPageState extends ConsumerState<ScriptPage> with LazyLoadState<Scri
|
||||
},
|
||||
child: ListView.builder(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
keyboardDismissBehavior:
|
||||
ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) {
|
||||
return searchCell(ref);
|
||||
@@ -87,82 +91,110 @@ class _ScriptPageState extends ConsumerState<ScriptPage> with LazyLoadState<Scri
|
||||
(item.title?.contains(_searchController.text) ?? false) ||
|
||||
(item.value?.contains(_searchController.text) ?? false) ||
|
||||
((item.children?.where((e) {
|
||||
return (e.title?.contains(_searchController.text) ?? false) || (e.value?.contains(_searchController.text) ?? false);
|
||||
return (e.title?.contains(_searchController.text) ??
|
||||
false) ||
|
||||
(e.value?.contains(_searchController.text) ??
|
||||
false);
|
||||
}).isNotEmpty ??
|
||||
false))) {
|
||||
return ColoredBox(
|
||||
color: ref.watch(themeProvider).themeColor.settingBgColor(),
|
||||
child: (item.children != null && item.children!.isNotEmpty)
|
||||
? ExpansionTile(
|
||||
title: Text(
|
||||
item.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref.watch(themeProvider).themeColor.descColor()
|
||||
: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
children: item.children!
|
||||
.where((element) {
|
||||
if (_searchController.text.isEmpty) {
|
||||
return true;
|
||||
}
|
||||
return (element.title?.contains(_searchController.text) ?? false) ||
|
||||
(element.value?.contains(_searchController.text) ?? false);
|
||||
})
|
||||
.map((e) => ListTile(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptDetail,
|
||||
arguments: {
|
||||
"title": e.title,
|
||||
"path": e.parent,
|
||||
color:
|
||||
ref.watch(themeProvider).themeColor.settingBgColor(),
|
||||
child:
|
||||
(item.children != null && item.children!.isNotEmpty)
|
||||
? ExpansionTile(
|
||||
title: Text(
|
||||
item.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.descColor()
|
||||
: ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.titleColor(),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
children: item.children!
|
||||
.where((element) {
|
||||
if (_searchController.text.isEmpty) {
|
||||
return true;
|
||||
}
|
||||
return (element.title?.contains(
|
||||
_searchController.text) ??
|
||||
false) ||
|
||||
(element.value?.contains(
|
||||
_searchController.text) ??
|
||||
false);
|
||||
})
|
||||
.map((e) => ListTile(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptDetail,
|
||||
arguments: {
|
||||
"title": e.title,
|
||||
"path": e.parent,
|
||||
},
|
||||
).then((value) {
|
||||
if (value != null &&
|
||||
value == true) {
|
||||
loadData();
|
||||
}
|
||||
});
|
||||
},
|
||||
).then((value) {
|
||||
if (value != null && value == true) {
|
||||
loadData();
|
||||
}
|
||||
});
|
||||
},
|
||||
title: Text(
|
||||
e.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref.watch(themeProvider).themeColor.descColor()
|
||||
: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
))
|
||||
.toList(),
|
||||
)
|
||||
: ListTile(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptDetail,
|
||||
arguments: {
|
||||
"title": item.title,
|
||||
"path": "",
|
||||
title: Text(
|
||||
e.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.descColor()
|
||||
: ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.titleColor(),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
))
|
||||
.toList(),
|
||||
)
|
||||
: ListTile(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptDetail,
|
||||
arguments: {
|
||||
"title": item.title,
|
||||
"path": "",
|
||||
},
|
||||
).then(
|
||||
(value) {
|
||||
if (value != null && value == true) {
|
||||
loadData();
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
).then(
|
||||
(value) {
|
||||
if (value != null && value == true) {
|
||||
loadData();
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
title: Text(
|
||||
item.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref.watch(themeProvider).themeColor.descColor()
|
||||
: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
fontSize: 16,
|
||||
title: Text(
|
||||
item.title ?? "",
|
||||
style: TextStyle(
|
||||
color: (item.disabled ?? false)
|
||||
? ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.descColor()
|
||||
: ref
|
||||
.watch(themeProvider)
|
||||
.themeColor
|
||||
.titleColor(),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return const SizedBox.shrink();
|
||||
@@ -209,144 +241,12 @@ class _ScriptPageState extends ConsumerState<ScriptPage> with LazyLoadState<Scri
|
||||
String scriptPath = "";
|
||||
|
||||
void addScript() {
|
||||
showCupertinoDialog(
|
||||
useRootNavigator: false,
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: const Text("新增脚本"),
|
||||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
const Text(
|
||||
"脚本名称:",
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 10,
|
||||
),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: TextField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.all(4),
|
||||
hintText: "请输入脚本名称",
|
||||
hintStyle: TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
autofocus: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
const Material(
|
||||
color: Colors.transparent,
|
||||
child: Text(
|
||||
"脚本所属文件夹:",
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
Material(
|
||||
color: Colors.transparent,
|
||||
child: DropdownButtonFormField<String>(
|
||||
items: list
|
||||
.where((element) => element.children?.isNotEmpty ?? false)
|
||||
.map((e) => DropdownMenuItem(
|
||||
value: e.value,
|
||||
child: SizedBox(
|
||||
width: MediaQuery.of(context).size.width / 2,
|
||||
child: Text(
|
||||
e.value ?? "",
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
))
|
||||
.toList()
|
||||
..insert(
|
||||
0,
|
||||
DropdownMenuItem(
|
||||
value: "",
|
||||
child: SizedBox(
|
||||
width: MediaQuery.of(context).size.width / 2,
|
||||
child: const Text(
|
||||
"根目录",
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
)),
|
||||
value: scriptPath,
|
||||
onChanged: (value) {
|
||||
scriptPath = value ?? "";
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
child: const Text(
|
||||
"取消",
|
||||
style: TextStyle(
|
||||
color: Color(0xff999999),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
child: Text(
|
||||
"确定",
|
||||
style: TextStyle(
|
||||
color: ref.watch(themeProvider).primaryColor,
|
||||
),
|
||||
),
|
||||
onPressed: () async {
|
||||
"提交中...".toast();
|
||||
HttpResponse<NullResponse> response = await Api.addScript(
|
||||
_nameController.text,
|
||||
scriptPath,
|
||||
"## created by 青龙客户端 ${DateTime.now()}\n\n",
|
||||
);
|
||||
if (response.success) {
|
||||
"提交成功".toast();
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptUpdate,
|
||||
arguments: {
|
||||
"title": _nameController.text,
|
||||
"path": scriptPath,
|
||||
"content": "## created by 青龙客户端 ${DateTime.now()}\n\n",
|
||||
},
|
||||
).then((value) {
|
||||
if (value != null && value == true) {
|
||||
_nameController.text = "";
|
||||
loadData();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
(response.message ?? "").toast();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
List<String?> paths = list
|
||||
.where((element) => element.children?.isNotEmpty ?? false)
|
||||
.map((e) => e.title)
|
||||
.toList();
|
||||
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => ScriptUploadPage(paths: paths)));
|
||||
}
|
||||
}
|
||||
|
||||
425
lib/module/others/scripts/script_upload_page.dart
Normal file
425
lib/module/others/scripts/script_upload_page.dart
Normal file
@@ -0,0 +1,425 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:qinglong_app/base/http/api.dart';
|
||||
import 'package:qinglong_app/base/http/http.dart';
|
||||
import 'package:qinglong_app/base/ql_app_bar.dart';
|
||||
import 'package:qinglong_app/base/routes.dart';
|
||||
import 'package:qinglong_app/base/theme.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:qinglong_app/module/others/scripts/script_code_detail_page.dart';
|
||||
import 'package:qinglong_app/module/task/task_bean.dart';
|
||||
import 'package:qinglong_app/utils/extension.dart';
|
||||
|
||||
/// @author NewTab
|
||||
|
||||
class ScriptUploadPage extends ConsumerStatefulWidget {
|
||||
final List<String?> paths;
|
||||
|
||||
const ScriptUploadPage({
|
||||
Key? key,
|
||||
required this.paths,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
ConsumerState<ScriptUploadPage> createState() => ScriptUploadPageState();
|
||||
}
|
||||
|
||||
class ScriptUploadPageState extends ConsumerState<ScriptUploadPage> {
|
||||
List<String> list = [];
|
||||
|
||||
final TextEditingController _nameController = TextEditingController();
|
||||
String scriptPath = "";
|
||||
File? file;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: QlAppBar(
|
||||
title: "新增脚本",
|
||||
actions: [
|
||||
InkWell(
|
||||
onTap: () {
|
||||
submit(context);
|
||||
},
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 15,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"提交",
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 15,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
const TitleWidget(
|
||||
"脚本名称",
|
||||
),
|
||||
TextField(
|
||||
controller: _nameController,
|
||||
decoration: InputDecoration(
|
||||
contentPadding: const EdgeInsets.fromLTRB(0, 5, 0, 5),
|
||||
hintText: "请输入脚本名称",
|
||||
hintStyle: TextStyle(
|
||||
fontSize: 14,
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
),
|
||||
),
|
||||
autofocus: false,
|
||||
),
|
||||
const SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
const TitleWidget(
|
||||
"脚本目录",
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
DropdownButtonFormField<String>(
|
||||
items: widget.paths
|
||||
.map((e) => DropdownMenuItem(
|
||||
value: e,
|
||||
child: SizedBox(
|
||||
width: MediaQuery.of(context).size.width / 2,
|
||||
child: Text(
|
||||
e ?? "",
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
))
|
||||
.toList()
|
||||
..insert(
|
||||
0,
|
||||
DropdownMenuItem(
|
||||
value: "",
|
||||
child: SizedBox(
|
||||
width: MediaQuery.of(context).size.width / 2,
|
||||
child: const Text(
|
||||
"根目录",
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
vertical: 15,
|
||||
),
|
||||
enabledBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Color(0xfff5f5f5),
|
||||
),
|
||||
),
|
||||
border: UnderlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Color(0xfff5f5f5),
|
||||
),
|
||||
),
|
||||
),
|
||||
value: scriptPath,
|
||||
onChanged: (value) {
|
||||
scriptPath = value ?? "";
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
const TitleWidget(
|
||||
"上传脚本",
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
Container(
|
||||
height: 80,
|
||||
alignment: Alignment.centerLeft,
|
||||
child: file == null ? addWidget() : addedWidget(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 50,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget addWidget() {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
if (result != null &&
|
||||
result.files.isNotEmpty &&
|
||||
result.files.single.path != null) {
|
||||
file = File(result.files.single.path!);
|
||||
|
||||
if (file == null) return;
|
||||
if (file!.lengthSync() > 5242880) {
|
||||
file = null;
|
||||
"最大支持上传5M的文件".toast();
|
||||
return;
|
||||
}
|
||||
|
||||
_nameController.text = getFileName();
|
||||
setState(() {});
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(
|
||||
top: 10,
|
||||
),
|
||||
width: 70,
|
||||
height: 70,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xfff3f5f7),
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
),
|
||||
child: Center(
|
||||
child: Image.asset(
|
||||
"assets/images/icon_add_file.png",
|
||||
width: 50,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget addedWidget(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
try {
|
||||
String content = await file!.readAsString();
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ScriptCodeDetailPage(
|
||||
title: getFileName(),
|
||||
content: content,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
e.toString().toast();
|
||||
}
|
||||
},
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: Container(
|
||||
height: 80,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 10,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Image.asset(
|
||||
getIconBySuffix(),
|
||||
width: 50,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
const SizedBox(
|
||||
width: 15,
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
getFileName(),
|
||||
style: TextStyle(
|
||||
color: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 5,
|
||||
),
|
||||
Text(
|
||||
getFileSize(file!.path, 1),
|
||||
style: TextStyle(
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
file = null;
|
||||
_nameController.text = "";
|
||||
setState(() {});
|
||||
},
|
||||
child: Icon(
|
||||
CupertinoIcons.clear,
|
||||
size: 20,
|
||||
color: ref.watch(themeProvider).themeColor.descColor(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String getFileSize(String filepath, int decimals) {
|
||||
var file = File(filepath);
|
||||
int bytes = file.lengthSync();
|
||||
if (bytes <= 0) return "0 B";
|
||||
const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
||||
var i = (log(bytes) / log(1024)).floor();
|
||||
return '${(bytes / pow(1024, i)).toStringAsFixed(decimals)} ${suffixes[i]}';
|
||||
}
|
||||
|
||||
String getFileName() {
|
||||
return basename(file!.path);
|
||||
}
|
||||
|
||||
String getIconBySuffix() {
|
||||
String end = file!.path;
|
||||
|
||||
if (end.endsWith(".py")) {
|
||||
return "assets/images/py.png";
|
||||
}
|
||||
if (end.endsWith(".js")) {
|
||||
return "assets/images/js.png";
|
||||
}
|
||||
if (end.endsWith(".ts")) {
|
||||
return "assets/images/ts.png";
|
||||
}
|
||||
if (end.endsWith(".json")) {
|
||||
return "assets/images/json.png";
|
||||
}
|
||||
if (end.endsWith(".sh")) {
|
||||
return "assets/images/shell.png";
|
||||
}
|
||||
return "assets/images/other.png";
|
||||
}
|
||||
|
||||
void submit(BuildContext context) async {
|
||||
try {
|
||||
if (_nameController.text.isEmpty) {
|
||||
"请输入文件名称".toast();
|
||||
return;
|
||||
}
|
||||
|
||||
if (file == null) {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.routeScriptAdd,
|
||||
arguments: {
|
||||
"title": _nameController.text,
|
||||
"path": scriptPath,
|
||||
},
|
||||
).then((value) {
|
||||
if (value != null && value == true) {
|
||||
Navigator.of(context).pop(true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
String content = await file!.readAsString();
|
||||
HttpResponse<NullResponse> response = await Api.addScript(
|
||||
_nameController.text,
|
||||
scriptPath,
|
||||
content,
|
||||
);
|
||||
if (response.success) {
|
||||
"提交成功".toast();
|
||||
|
||||
String command =
|
||||
"task $scriptPath${(scriptPath.isNotEmpty) ? separator : ""}${getFileName()} ";
|
||||
|
||||
String? cron = getCronString(content, getFileName());
|
||||
Navigator.of(context).popAndPushNamed(
|
||||
Routes.routeAddTask,
|
||||
arguments: TaskBean(
|
||||
name: _nameController.text,
|
||||
command: command,
|
||||
schedule: cron,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
(response.message ?? "").toast();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
e.toString().toast();
|
||||
}
|
||||
}
|
||||
|
||||
static String? getCronString(String pre, String fileName) {
|
||||
String reg =
|
||||
"([\\d\\*]*[\\*-\\/,\\d]*[\\d\\*] ){4,5}[\\d\\*]*[\\*-\\/,\\d]*[\\d\\*]( |,|\").*$fileName";
|
||||
RegExp regExp = RegExp(reg);
|
||||
RegExpMatch? result = regExp.firstMatch(pre);
|
||||
return result?[0]?.replaceAll(fileName, "").trim();
|
||||
}
|
||||
}
|
||||
|
||||
class TitleWidget extends ConsumerWidget {
|
||||
final String title;
|
||||
final bool required;
|
||||
|
||||
const TitleWidget(
|
||||
this.title, {
|
||||
Key? key,
|
||||
this.required = false,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, ref) {
|
||||
return RichText(
|
||||
text: TextSpan(
|
||||
text: !required ? "" : "* ",
|
||||
style: const TextStyle(
|
||||
color: Color(0xFFF02D2D),
|
||||
),
|
||||
children: <TextSpan>[
|
||||
TextSpan(
|
||||
text: title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: ref.watch(themeProvider).themeColor.titleColor(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user