From b02e3e17c769603402c6cb30510953b6893a8b24 Mon Sep 17 00:00:00 2001 From: wanghongenpin Date: Wed, 27 Nov 2024 01:57:57 +0800 Subject: [PATCH] Background color of QR code image --- lib/l10n/app_en.arb | 1 - lib/l10n/app_zh.arb | 1 - lib/ui/component/toolbox/qr_code_page.dart | 27 ++++++++++++++++------ lib/ui/mobile/setting/ssl.dart | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 05cb36a..7cf9fb7 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -188,7 +188,6 @@ "installCaLocal": "Install Certificate to Local-Machine", "downloadRootCa": "Download Certificate", "downloadRootCaNote": "Note: If you set the default browser to other than Safari, click this line to copy and paste the link to Safari browser", - "copyRootCaSuccess": "Copy link success", "generateCA": "Generate new root certificate", "generateCADescribe": "Are you sure you want to generate a new root certificate? If confirmed,\nYou need to reinstall and trust the new certificate", "resetDefaultCA": "Reset Default Root Certificate", diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb index b2a8599..64b9a9c 100644 --- a/lib/l10n/app_zh.arb +++ b/lib/l10n/app_zh.arb @@ -189,7 +189,6 @@ "installCaLocal": "安装根证书到本机", "downloadRootCa": "下载根证书", "downloadRootCaNote": "注意:如果您将默认浏览器设置为 Safari 以外的浏览器,请单击此行复制并粘贴 Safari 浏览器的链接", - "copyRootCaSuccess": "复制链接成功", "generateCA": "重新生成根证书", "generateCADescribe": "您确定要生成新的根证书吗? 如果确认,\n则需要重新安装并信任新的证书", "resetDefaultCA": "重置默认根证书", diff --git a/lib/ui/component/toolbox/qr_code_page.dart b/lib/ui/component/toolbox/qr_code_page.dart index 6fdfa86..ad38340 100644 --- a/lib/ui/component/toolbox/qr_code_page.dart +++ b/lib/ui/component/toolbox/qr_code_page.dart @@ -16,10 +16,12 @@ import 'dart:async'; import 'dart:io'; +import 'dart:ui' as ui; import 'package:desktop_multi_window/desktop_multi_window.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_qr_reader/flutter_qr_reader.dart'; @@ -59,7 +61,7 @@ class _QrCodePageState extends State with SingleTickerProviderStateM if (Platforms.isMobile()) { tabController = TabController(initialIndex: 0, length: tabs.length, vsync: this); } - + if (Platforms.isDesktop() && widget.windowId != null) { HardwareKeyboard.instance.addHandler(onKeyEvent); } @@ -244,6 +246,7 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin var errorCorrectLevel = QrErrorCorrectLevel.M; String? data; TextEditingController inputData = TextEditingController(); + final GlobalKey imageKey = GlobalKey(); AppLocalizations get localizations => AppLocalizations.of(context)!; @@ -316,7 +319,14 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin SizedBox(width: 20), ]), SizedBox(height: 5), - Center(child: QrImageView(size: 300, data: inputData.text, errorCorrectionLevel: errorCorrectLevel)), + Center( + child: RepaintBoundary( + key: imageKey, + child: QrImageView( + size: 300, + data: inputData.text, + backgroundColor: Colors.white, + errorCorrectionLevel: errorCorrectLevel))), ], ), SizedBox(height: 15), @@ -331,6 +341,7 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin if (Platforms.isMobile()) { var imageBytes = await toImageBytes(); + if (imageBytes == null) return; String? path = await ImagePickers.saveByteDataImageToGallery(imageBytes); if (path != null && mounted) { FlutterToastr.show(localizations.saveSuccess, context, duration: 2, rootNavigator: true); @@ -343,6 +354,8 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin if (path == null) return; var imageBytes = await toImageBytes(); + if (imageBytes == null) return; + await File(path).writeAsBytes(imageBytes); if (mounted) { FlutterToastr.show(localizations.saveSuccess, context, duration: 2); @@ -350,10 +363,10 @@ class _QrEncodeState extends State<_QrEncode> with AutomaticKeepAliveClientMixin } } - Future toImageBytes() async { - QrPainter painter = QrPainter(data: data!, errorCorrectionLevel: errorCorrectLevel, version: QrVersions.auto); - var imageData = await painter.toImageData(300); - - return imageData!.buffer.asUint8List(); + Future toImageBytes() async { + RenderRepaintBoundary render = imageKey.currentContext!.findRenderObject() as RenderRepaintBoundary; + ui.Image image = await render.toImage(); + ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png); + return byteData?.buffer.asUint8List(); } } diff --git a/lib/ui/mobile/setting/ssl.dart b/lib/ui/mobile/setting/ssl.dart index e727fe6..6817288 100644 --- a/lib/ui/mobile/setting/ssl.dart +++ b/lib/ui/mobile/setting/ssl.dart @@ -232,7 +232,7 @@ class _MobileSslState extends State { if (!mounted) { return; } - FlutterToastr.show(localizations.copyRootCaSuccess, context); + FlutterToastr.show(localizations.copied, context); }); }