fix: 增加防丢图片修改
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:gal/gal.dart';
|
||||
import 'package:kaer_with_panels/app/utils/kr_common_util.dart';
|
||||
@@ -10,10 +11,9 @@ import 'package:kaer_with_panels/app/widgets/dialogs/hi_dialog.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class HIAntiLostController extends GetxController {
|
||||
final GlobalKey repaintKey = GlobalKey();
|
||||
|
||||
Future<void> saveImage() async {
|
||||
KRCommonUtil.kr_showLoading(message: "正在保存...");
|
||||
// KRCommonUtil.kr_showLoading(message: "正在保存...");
|
||||
try {
|
||||
// Check permission
|
||||
final hasAccess = await Gal.hasAccess();
|
||||
@@ -21,43 +21,23 @@ class HIAntiLostController extends GetxController {
|
||||
await Gal.requestAccess();
|
||||
}
|
||||
|
||||
// Capture image
|
||||
RenderRepaintBoundary? boundary = repaintKey.currentContext
|
||||
?.findRenderObject() as RenderRepaintBoundary?;
|
||||
// Load image from assets
|
||||
final ByteData data =
|
||||
await rootBundle.load('assets/images/lost-share-image.png');
|
||||
final Uint8List pngBytes = data.buffer.asUint8List();
|
||||
|
||||
if (boundary == null) {
|
||||
throw Exception("Cannot find boundary");
|
||||
await Gal.putImageBytes(pngBytes);
|
||||
|
||||
String message = "已保存至系统相册";
|
||||
if (Platform.isMacOS) {
|
||||
message = "已保存至系统【照片】应用";
|
||||
} else if (Platform.isWindows) {
|
||||
message = "已保存至系统【图片】文件夹";
|
||||
}
|
||||
|
||||
// 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?;
|
||||
}
|
||||
|
||||
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("保存成功");
|
||||
|
||||
String message = "已保存至系统相册";
|
||||
if (Platform.isMacOS) {
|
||||
message = "已保存至系统【照片】应用";
|
||||
} else if (Platform.isWindows) {
|
||||
message = "已保存至系统【图片】文件夹";
|
||||
}
|
||||
|
||||
HIDialog.show(
|
||||
message: message,
|
||||
);
|
||||
} else {
|
||||
KRCommonUtil.kr_showToast("生成图片失败");
|
||||
}
|
||||
HIDialog.show(
|
||||
message: message,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint("Save image error: $e");
|
||||
if (e is GalException) {
|
||||
@@ -66,7 +46,7 @@ class HIAntiLostController extends GetxController {
|
||||
KRCommonUtil.kr_showToast("保存失败");
|
||||
}
|
||||
} finally {
|
||||
KRCommonUtil.kr_hideLoading();
|
||||
//KRCommonUtil.kr_hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user