feat: 增加防丢失页面海报生成
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -5,6 +6,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gal/gal.dart';
|
||||
import 'package:kaer_with_panels/app/utils/kr_common_util.dart';
|
||||
import 'package:kaer_with_panels/app/widgets/dialogs/hi_dialog.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class HIAntiLostController extends GetxController {
|
||||
@@ -20,9 +22,9 @@ class HIAntiLostController extends GetxController {
|
||||
}
|
||||
|
||||
// Capture image
|
||||
RenderRepaintBoundary? boundary =
|
||||
repaintKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
|
||||
|
||||
RenderRepaintBoundary? boundary = repaintKey.currentContext
|
||||
?.findRenderObject() as RenderRepaintBoundary?;
|
||||
|
||||
if (boundary == null) {
|
||||
throw Exception("Cannot find boundary");
|
||||
}
|
||||
@@ -30,26 +32,38 @@ class HIAntiLostController extends GetxController {
|
||||
// Check if the boundary needs layout (sometimes Offstage needs a frame)
|
||||
if (boundary.debugNeedsPaint) {
|
||||
await Future.delayed(const Duration(milliseconds: 20));
|
||||
boundary = repaintKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
|
||||
boundary = repaintKey.currentContext?.findRenderObject()
|
||||
as RenderRepaintBoundary?;
|
||||
}
|
||||
|
||||
ui.Image image = await boundary!.toImage(pixelRatio: 3.0);
|
||||
ByteData? byteData =
|
||||
await image.toByteData(format: ui.ImageByteFormat.png);
|
||||
|
||||
|
||||
if (byteData != null) {
|
||||
final Uint8List pngBytes = byteData.buffer.asUint8List();
|
||||
await Gal.putImageBytes(pngBytes);
|
||||
KRCommonUtil.kr_showToast("保存成功");
|
||||
// KRCommonUtil.kr_showToast("保存成功");
|
||||
|
||||
String message = "已保存至系统相册";
|
||||
if (Platform.isMacOS) {
|
||||
message = "已保存至系统【照片】应用";
|
||||
} else if (Platform.isWindows) {
|
||||
message = "已保存至系统【图片】文件夹";
|
||||
}
|
||||
|
||||
HIDialog.show(
|
||||
message: message,
|
||||
);
|
||||
} else {
|
||||
KRCommonUtil.kr_showToast("生成图片失败");
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Save image error: $e");
|
||||
if (e is GalException) {
|
||||
KRCommonUtil.kr_showToast("保存失败: No Permission");
|
||||
KRCommonUtil.kr_showToast("保存失败: No Permission");
|
||||
} else {
|
||||
KRCommonUtil.kr_showToast("保存失败");
|
||||
KRCommonUtil.kr_showToast("保存失败");
|
||||
}
|
||||
} finally {
|
||||
KRCommonUtil.kr_hideLoading();
|
||||
@@ -67,7 +81,7 @@ class HIAntiLostController extends GetxController {
|
||||
} else {
|
||||
// Fallback to web
|
||||
if (!await launchUrl(webUrl, mode: LaunchMode.externalApplication)) {
|
||||
KRCommonUtil.kr_showToast("无法打开链接");
|
||||
KRCommonUtil.kr_showToast("无法打开链接");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -75,7 +89,7 @@ class HIAntiLostController extends GetxController {
|
||||
// Fallback to web if anything goes wrong
|
||||
try {
|
||||
if (!await launchUrl(webUrl, mode: LaunchMode.externalApplication)) {
|
||||
KRCommonUtil.kr_showToast("无法打开链接");
|
||||
KRCommonUtil.kr_showToast("无法打开链接");
|
||||
}
|
||||
} catch (e) {
|
||||
KRCommonUtil.kr_showToast("无法打开链接");
|
||||
|
||||
Reference in New Issue
Block a user