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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:kaer_with_panels/app/widgets/hi_base_scaffold.dart';
|
||||
import 'package:qr_flutter/qr_flutter.dart';
|
||||
import 'package:kaer_with_panels/app/widgets/kr_local_image.dart';
|
||||
import 'package:kaer_with_panels/app/modules/hi_anti_lost/controllers/hi_anti_lost_controller.dart';
|
||||
import 'package:kaer_with_panels/app/modules/hi_anti_lost/widgets/hi_anti_lost_share_card.dart';
|
||||
|
||||
class HIAntiLostView extends GetView<HIAntiLostController> {
|
||||
const HIAntiLostView({super.key});
|
||||
@@ -18,70 +16,19 @@ class HIAntiLostView extends GetView<HIAntiLostController> {
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
Container(
|
||||
width: double.infinity,
|
||||
margin: EdgeInsets.symmetric(horizontal: 40.w),
|
||||
padding: EdgeInsets.symmetric(vertical: 16.w, horizontal: 0.w),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black,
|
||||
borderRadius: BorderRadius.circular(25.r),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).primaryColor, width: 4),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
'保存二维码,防止失联',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).primaryColor,
|
||||
fontSize: 24.sp,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 25.w),
|
||||
Text(
|
||||
'建议保存Hi快VPN防丢二维码到相册\n永久保障您的互联网自由',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).primaryColor,
|
||||
fontSize: 14.sp,
|
||||
height: 1.5,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 10.w),
|
||||
// QR Code
|
||||
LayoutBuilder(builder: (context, constraints) {
|
||||
// Use a reasonable size for the QR code
|
||||
return Container(
|
||||
width: 124.w,
|
||||
height: 124.w,
|
||||
child: QrImageView(
|
||||
data:
|
||||
'https://github.com/HiFastVPN/HiFastVPN', // Replace with real URL
|
||||
version: QrVersions.auto,
|
||||
backgroundColor: Colors.transparent,
|
||||
eyeStyle: QrEyeStyle(
|
||||
eyeShape: QrEyeShape.square,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
dataModuleStyle: QrDataModuleStyle(
|
||||
dataModuleShape: QrDataModuleShape.square,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
embeddedImage: const AssetImage(
|
||||
'assets/images/lost-poster-logo-white.png'),
|
||||
embeddedImageStyle: QrEmbeddedImageStyle(
|
||||
size: Size(36.w, 36.w),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
KrLocalImage(
|
||||
imageName: 'lost-image',
|
||||
imageType: ImageType.png,
|
||||
width: 298.w,
|
||||
height: 385.w,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 10.w),
|
||||
SizedBox(height: 30.w),
|
||||
|
||||
// Save Button
|
||||
GestureDetector(
|
||||
@@ -129,14 +76,6 @@ class HIAntiLostView extends GetView<HIAntiLostController> {
|
||||
),
|
||||
),
|
||||
|
||||
// 3. Invisible Share Card for Generation
|
||||
Positioned(
|
||||
left: 10000,
|
||||
child: RepaintBoundary(
|
||||
key: controller.repaintKey,
|
||||
child: const HIAntiLostShareCard(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:qr_flutter/qr_flutter.dart';
|
||||
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
|
||||
|
||||
class HIAntiLostShareCard extends StatelessWidget {
|
||||
const HIAntiLostShareCard({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Fixed dimensions for the generated image
|
||||
final double cardWidth = 375.0;
|
||||
final double cardHeight = 812.0;
|
||||
|
||||
return Container(
|
||||
width: cardWidth,
|
||||
height: cardHeight,
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/images/lost-poster-bg.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
top: 32,
|
||||
right: 30,
|
||||
child: SizedBox(
|
||||
width: 108,
|
||||
height: 108,
|
||||
child: QrImageView(
|
||||
data:
|
||||
'https://github.com/HiFastVPN/HiFastVPN', // Replace with actual URL
|
||||
version: QrVersions.auto,
|
||||
size: 108,
|
||||
padding: EdgeInsets.zero,
|
||||
backgroundColor: Colors.transparent,
|
||||
eyeStyle: const QrEyeStyle(
|
||||
eyeShape: QrEyeShape.square,
|
||||
color: Colors.white,
|
||||
),
|
||||
dataModuleStyle: const QrDataModuleStyle(
|
||||
dataModuleShape: QrDataModuleShape.square,
|
||||
color: Colors.white,
|
||||
),
|
||||
embeddedImage: const AssetImage(
|
||||
'assets/images/lost-poster-logo-white.png'),
|
||||
embeddedImageStyle: const QrEmbeddedImageStyle(
|
||||
size: Size(42, 42),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user