Skip to content

Commit

Permalink
Background color of QR code image
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghongenpin committed Nov 26, 2024
1 parent 6aacdfb commit b02e3e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 0 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@
"installCaLocal": "安装根证书到本机",
"downloadRootCa": "下载根证书",
"downloadRootCaNote": "注意:如果您将默认浏览器设置为 Safari 以外的浏览器,请单击此行复制并粘贴 Safari 浏览器的链接",
"copyRootCaSuccess": "复制链接成功",
"generateCA": "重新生成根证书",
"generateCADescribe": "您确定要生成新的根证书吗? 如果确认,\n则需要重新安装并信任新的证书",
"resetDefaultCA": "重置默认根证书",
Expand Down
27 changes: 20 additions & 7 deletions lib/ui/component/toolbox/qr_code_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,7 +61,7 @@ class _QrCodePageState extends State<QrCodePage> with SingleTickerProviderStateM
if (Platforms.isMobile()) {
tabController = TabController(initialIndex: 0, length: tabs.length, vsync: this);
}

if (Platforms.isDesktop() && widget.windowId != null) {
HardwareKeyboard.instance.addHandler(onKeyEvent);
}
Expand Down Expand Up @@ -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)!;

Expand Down Expand Up @@ -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),
Expand All @@ -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);
Expand All @@ -343,17 +354,19 @@ 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);
}
}
}

Future<Uint8List> toImageBytes() async {
QrPainter painter = QrPainter(data: data!, errorCorrectionLevel: errorCorrectLevel, version: QrVersions.auto);
var imageData = await painter.toImageData(300);

return imageData!.buffer.asUint8List();
Future<Uint8List?> 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();
}
}
2 changes: 1 addition & 1 deletion lib/ui/mobile/setting/ssl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class _MobileSslState extends State<MobileSslWidget> {
if (!mounted) {
return;
}
FlutterToastr.show(localizations.copyRootCaSuccess, context);
FlutterToastr.show(localizations.copied, context);
});
}

Expand Down

0 comments on commit b02e3e1

Please sign in to comment.