pikapika/lib/basic/Common.dart

331 lines
9.1 KiB
Dart
Raw Permalink Normal View History

2022-07-01 05:39:08 +00:00
import 'dart:async';
import 'dart:io';
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
2022-07-01 05:39:08 +00:00
import 'package:uni_links/uni_links.dart';
import 'package:uri_to_file/uri_to_file.dart';
2021-09-29 23:57:09 +00:00
2022-07-04 02:02:09 +00:00
import '../screens/ComicInfoScreen.dart';
2022-07-01 05:39:08 +00:00
import '../screens/DownloadOnlyImportScreen.dart';
import '../screens/PkzArchiveScreen.dart';
2023-01-31 10:50:51 +00:00
import 'config/IconLoading.dart';
import 'config/TimeOffsetHour.dart';
2021-10-21 04:08:51 +00:00
/// 默认的图片尺寸
2021-09-29 23:57:09 +00:00
double coverWidth = 210;
double coverHeight = 315;
String categoryTitle(String? categoryTitle) {
return categoryTitle ?? "全分类";
}
/// 显示一个toast
void defaultToast(BuildContext context, String title) {
showToast(
title,
context: context,
position: StyledToastPosition.center,
animation: StyledToastAnimation.scale,
reverseAnimation: StyledToastAnimation.fade,
2022-03-17 03:31:25 +00:00
duration: const Duration(seconds: 4),
animDuration: const Duration(seconds: 1),
2021-09-29 23:57:09 +00:00
curve: Curves.elasticOut,
reverseCurve: Curves.linear,
);
}
/// 显示一个确认框, 用户关闭弹窗以及选择否都会返回false, 仅当用户选择确定时返回true
Future<bool> confirmDialog(
BuildContext context, String title, String content) async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
2022-03-17 03:31:25 +00:00
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[Text(content)],
2021-09-29 23:57:09 +00:00
),
),
actions: <Widget>[
2022-03-17 03:31:25 +00:00
MaterialButton(
child: const Text('取消'),
2021-09-29 23:57:09 +00:00
onPressed: () {
Navigator.of(context).pop(false);
},
),
2022-03-17 03:31:25 +00:00
MaterialButton(
child: const Text('确定'),
2021-09-29 23:57:09 +00:00
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
)) ??
false;
}
/// 显示一个消息提示框
Future alertDialog(BuildContext context, String title, String content) {
return showDialog(
2022-03-17 03:31:25 +00:00
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(content),
],
),
),
actions: <Widget>[
MaterialButton(
child: const Text('确定'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
2021-09-29 23:57:09 +00:00
}
/// stream-filter的替代方法
List<T> filteredList<T>(List<T> list, bool Function(T) filter) {
List<T> result = [];
2022-03-17 03:31:25 +00:00
for (var element in list) {
2021-09-29 23:57:09 +00:00
if (filter(element)) {
result.add(element);
}
2022-03-17 03:31:25 +00:00
}
2021-09-29 23:57:09 +00:00
return result;
}
/// 创建一个单选对话框, 用户取消选择返回null, 否则返回所选内容
Future<T?> chooseListDialog<T>(
2021-11-06 11:25:44 +00:00
BuildContext context, String title, List<T> items,
{String? tips}) async {
2021-09-29 23:57:09 +00:00
return showDialog<T>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(title),
2022-03-01 12:00:27 +00:00
children: [
...items.map((e) => SimpleDialogOption(
onPressed: () {
Navigator.of(context).pop(e);
},
child: Text('$e'),
)),
...tips != null
? [
Container(
padding: const EdgeInsets.fromLTRB(15, 5, 15, 15),
child: Text(tips),
),
]
: [],
],
2021-09-29 23:57:09 +00:00
);
},
);
}
/// 创建一个单选对话框, 用户取消选择返回null, 否则返回所选内容(value)
Future<T?> chooseMapDialog<T>(
BuildContext buildContext, Map<String, T> values, String title) async {
return await showDialog<T>(
context: buildContext,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(title),
children: values.entries
.map((e) => SimpleDialogOption(
child: Text(e.key),
onPressed: () {
Navigator.of(context).pop(e.value);
},
))
.toList(),
);
},
);
}
/// 输入对话框1
2022-07-01 05:39:08 +00:00
var _controller =
TextEditingController.fromValue(const TextEditingValue(text: ''));
2021-09-29 23:57:09 +00:00
2021-12-15 04:22:40 +00:00
Future<String?> displayTextInputDialog(BuildContext context,
2022-02-24 16:08:18 +00:00
{String? title,
String src = "",
String? hint,
String? desc,
bool isPasswd = false}) {
2021-09-29 23:57:09 +00:00
_controller.text = src;
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
2021-12-15 04:22:40 +00:00
title: title == null ? null : Text(title),
2021-09-29 23:57:09 +00:00
content: SingleChildScrollView(
child: ListBody(
children: [
TextField(
controller: _controller,
decoration: InputDecoration(hintText: hint),
obscureText: isPasswd,
obscuringCharacter: '\u2022',
2021-09-29 23:57:09 +00:00
),
2021-12-15 04:22:40 +00:00
...(desc == null
? []
: [
Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.only(top: 20, bottom: 10),
2021-12-15 04:22:40 +00:00
child: Text(
desc,
style: TextStyle(
fontSize: 12,
color: Theme.of(context)
.textTheme
.bodyText1
?.color
?.withOpacity(.5)),
),
)
]),
2021-09-29 23:57:09 +00:00
],
),
),
actions: <Widget>[
MaterialButton(
2022-03-25 14:57:30 +00:00
child: const Text('取消'),
2021-09-29 23:57:09 +00:00
onPressed: () {
Navigator.of(context).pop();
},
),
MaterialButton(
2022-03-17 03:31:25 +00:00
child: const Text('确认'),
2021-09-29 23:57:09 +00:00
onPressed: () {
Navigator.of(context).pop(_controller.text);
},
),
],
);
},
);
}
/// 将字符串前面加0直至满足len位
String add0(int num, int len) {
var rsp = "$num";
while (rsp.length < len) {
rsp = "0$rsp";
}
return rsp;
}
/// 格式化时间 2012-34-56
String formatTimeToDate(String str) {
try {
var c = DateTime.parse(str).add(Duration(hours: currentTimeOffsetHour()));
2021-09-29 23:57:09 +00:00
return "${add0(c.year, 4)}-${add0(c.month, 2)}-${add0(c.day, 2)}";
} catch (e) {
return "-";
}
}
/// 格式化时间 2012-34-56 12:34:56
String formatTimeToDateTime(String str) {
try {
var c = DateTime.parse(str).add(Duration(hours: currentTimeOffsetHour()));
2021-09-29 23:57:09 +00:00
return "${add0(c.year, 4)}-${add0(c.month, 2)}-${add0(c.day, 2)} ${add0(c.hour, 2)}:${add0(c.minute, 2)}";
} catch (e) {
return "-";
}
}
/// 输入对话框2
final TextEditingController _textEditController =
TextEditingController(text: '');
Future<String?> inputString(BuildContext context, String title,
2022-02-24 16:08:18 +00:00
{String hint = "", String? defaultValue}) async {
if (defaultValue != null) {
_textEditController.text = defaultValue;
} else {
_textEditController.clear();
}
2021-09-29 23:57:09 +00:00
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Card(
child: SingleChildScrollView(
child: ListBody(
children: [
Text(title),
2022-03-17 03:31:25 +00:00
TextField(
controller: _textEditController,
2022-07-01 05:39:08 +00:00
decoration: InputDecoration(
2022-03-17 03:31:25 +00:00
labelText: hint,
2021-09-29 23:57:09 +00:00
),
),
],
),
),
),
actions: <Widget>[
MaterialButton(
onPressed: () {
Navigator.pop(context);
},
2022-03-17 03:31:25 +00:00
child: const Text('取消'),
2021-09-29 23:57:09 +00:00
),
MaterialButton(
onPressed: () {
Navigator.pop(context, _textEditController.text);
},
2022-03-17 03:31:25 +00:00
child: const Text('确定'),
2021-09-29 23:57:09 +00:00
),
],
);
},
);
}
2022-07-01 05:39:08 +00:00
StreamSubscription<String?> linkSubscript(BuildContext context) {
return linkStream.listen((uri) async {
if (uri == null) return;
2022-07-04 02:02:09 +00:00
if (RegExp(r"^pika://comic/([0-9A-z]+)/$").allMatches(uri).isNotEmpty) {
String comicId = RegExp(r"^pika://comic/([0-9A-z]+)/$")
.allMatches(uri)
.first
.group(1)!;
Navigator.of(context).push(
2023-01-31 10:50:51 +00:00
mixRoute(
2022-07-04 02:02:09 +00:00
builder: (BuildContext context) => ComicInfoScreen(comicId: comicId),
),
);
} else if (RegExp(r"^.*\.pkz$").allMatches(uri).isNotEmpty) {
2022-07-01 05:39:08 +00:00
File file = await toFile(uri);
Navigator.of(context).push(
2023-01-31 10:50:51 +00:00
mixRoute(
2022-07-01 05:39:08 +00:00
builder: (BuildContext context) =>
PkzArchiveScreen(pkzPath: file.path),
),
);
} else if (RegExp(r"^.*\.((pki)|(zip))$").allMatches(uri).isNotEmpty) {
File file = await toFile(uri);
Navigator.of(context).push(
2023-01-31 10:50:51 +00:00
mixRoute(
2022-07-01 05:39:08 +00:00
builder: (BuildContext context) =>
DownloadOnlyImportScreen(path: file.path),
),
);
}
});
}