feat: 增加防丢失页面

This commit is contained in:
2026-01-12 06:49:03 -08:00
parent 0474f3c9d9
commit 26f6c10557
16 changed files with 390 additions and 0 deletions
@@ -0,0 +1,85 @@
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
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:url_launcher/url_launcher.dart';
class HIAntiLostController extends GetxController {
final GlobalKey repaintKey = GlobalKey();
Future<void> saveImage() async {
KRCommonUtil.kr_showLoading(message: "正在保存...");
try {
// Check permission
final hasAccess = await Gal.hasAccess();
if (!hasAccess) {
await Gal.requestAccess();
}
// Capture image
RenderRepaintBoundary? boundary =
repaintKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
if (boundary == null) {
throw Exception("Cannot find boundary");
}
// 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("保存成功");
} else {
KRCommonUtil.kr_showToast("生成图片失败");
}
} catch (e) {
debugPrint("Save image error: $e");
if (e is GalException) {
KRCommonUtil.kr_showToast("保存失败: No Permission");
} else {
KRCommonUtil.kr_showToast("保存失败");
}
} finally {
KRCommonUtil.kr_hideLoading();
}
}
void openTwitter() async {
// Try Deep Link first
final Uri appUrl = Uri.parse('twitter://user?screen_name=hifasttech');
final Uri webUrl = Uri.parse('https://x.com/hifasttech');
try {
if (await canLaunchUrl(appUrl)) {
await launchUrl(appUrl);
} else {
// Fallback to web
if (!await launchUrl(webUrl, mode: LaunchMode.externalApplication)) {
KRCommonUtil.kr_showToast("无法打开链接");
}
}
} catch (e) {
debugPrint("Open Twitter error: $e");
// Fallback to web if anything goes wrong
try {
if (!await launchUrl(webUrl, mode: LaunchMode.externalApplication)) {
KRCommonUtil.kr_showToast("无法打开链接");
}
} catch (e) {
KRCommonUtil.kr_showToast("无法打开链接");
}
}
}
}