pikapika/lib/basic/Cross.dart

109 lines
3.0 KiB
Dart
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
/// 与平台交互的操作
import 'dart:io';
2022-02-25 10:46:11 +00:00
2021-09-29 23:57:09 +00:00
import 'package:clipboard/clipboard.dart';
2022-04-30 04:59:03 +00:00
import 'package:file_picker/file_picker.dart';
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
2024-04-02 13:06:03 +00:00
import 'package:pikapika/basic/config/CopySkipConfirm.dart';
2023-04-12 07:53:37 +00:00
import 'package:pikapika/basic/config/Platform.dart';
2021-09-29 23:57:09 +00:00
import 'package:url_launcher/url_launcher.dart';
2022-02-25 10:46:11 +00:00
2021-09-29 23:57:09 +00:00
import 'Method.dart';
2021-10-07 03:47:28 +00:00
import 'config/ChooserRoot.dart';
2021-09-29 23:57:09 +00:00
/// 复制内容到剪切板
void copyToClipBoard(BuildContext context, String string) {
2021-12-28 01:28:14 +00:00
FlutterClipboard.copy(string);
defaultToast(context, "已复制到剪切板");
2021-09-29 23:57:09 +00:00
}
2024-04-02 13:06:03 +00:00
void copyToClipBoardTips(BuildContext context, String string) {
FlutterClipboard.copy(string);
defaultToast(context, "已复制到剪切板 :\n$string");
}
2021-09-29 23:57:09 +00:00
/// 打开web页面
Future<dynamic> openUrl(String url) async {
if (await canLaunch(url)) {
await launch(
url,
forceSafariVC: false,
);
}
}
/// 保存图片
Future<dynamic> saveImage(String path, BuildContext context) async {
Future? future;
if (Platform.isIOS) {
future = method.iosSaveFileToImage(path);
} else if (Platform.isAndroid) {
future = _saveImageAndroid(path, context);
} else if (Platform.isWindows || Platform.isMacOS || Platform.isLinux) {
String? folder = await chooseFolder(context);
if (folder != null) {
future = method.convertImageToJPEG100(path, folder);
}
} else {
defaultToast(context, '暂不支持该平台');
2021-11-12 05:41:26 +00:00
return;
}
if (future == null) {
defaultToast(context, '保存取消');
return;
}
try {
await future;
defaultToast(context, '保存成功');
} catch (e, s) {
print("$e\n$s");
defaultToast(context, '保存失败');
2021-09-29 23:57:09 +00:00
}
}
2021-10-21 04:08:51 +00:00
/// 保存图片且保持静默, 用于批量导出到相册
2021-10-14 10:12:36 +00:00
Future<dynamic> saveImageQuiet(String path, BuildContext context) async {
if (Platform.isIOS) {
return method.iosSaveFileToImage(path);
} else if (Platform.isAndroid) {
return _saveImageAndroid(path, context);
} else {
throw Exception("only mobile");
}
}
2021-09-29 23:57:09 +00:00
Future<dynamic> _saveImageAndroid(String path, BuildContext context) async {
2023-04-12 07:53:37 +00:00
late bool g;
if (androidVersion < 30) {
g = await Permission.storage.request().isGranted;
2024-04-02 13:06:03 +00:00
} else {
2023-04-12 07:53:37 +00:00
g = await Permission.manageExternalStorage.request().isGranted;
}
if (!g) {
2021-09-29 23:57:09 +00:00
return;
}
return method.androidSaveFileToImage(path);
}
/// 选择一个文件夹用于保存文件
Future<String?> chooseFolder(BuildContext context) async {
2022-04-30 04:59:03 +00:00
return FilePicker.platform.getDirectoryPath(
dialogTitle: "选择一个文件夹, 将文件保存到这里",
2024-04-02 13:06:03 +00:00
initialDirectory:
Directory.fromUri(Uri.file(await currentChooserRoot())).absolute.path,
2021-09-29 23:57:09 +00:00
);
}
2021-10-21 04:08:51 +00:00
/// 复制对话框
2021-09-29 23:57:09 +00:00
void confirmCopy(BuildContext context, String content) async {
2024-04-02 13:06:03 +00:00
if (copySkipConfirm()) {
copyToClipBoardTips(context, content);
} else {
if (await confirmDialog(context, "复制", content)) {
copyToClipBoard(context, content);
}
2021-09-29 23:57:09 +00:00
}
}