stack_wallet/lib/pages/receive_view/addresses/address_qr_popup.dart

207 lines
6.2 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2023-02-02 21:37:59 +00:00
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;
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_svg/svg.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:share_plus/share_plus.dart';
2024-05-27 23:56:22 +00:00
import '../../../notifications/show_flush_bar.dart';
import '../../../themes/stack_colors.dart';
import '../../../utilities/address_utils.dart';
import '../../../utilities/assets.dart';
import '../../../utilities/clipboard_interface.dart';
import '../../../utilities/text_styles.dart';
import '../../../utilities/util.dart';
import '../../../wallets/crypto_currency/crypto_currency.dart';
import '../../../widgets/desktop/primary_button.dart';
import '../../../widgets/desktop/secondary_button.dart';
import '../../../widgets/stack_dialog.dart';
2023-02-02 21:37:59 +00:00
class AddressQrPopup extends StatefulWidget {
const AddressQrPopup({
2024-05-15 21:20:45 +00:00
super.key,
2023-02-03 19:22:21 +00:00
required this.addressString,
2023-02-02 21:37:59 +00:00
required this.coin,
this.clipboard = const ClipboardWrapper(),
2024-05-15 21:20:45 +00:00
});
2023-02-02 21:37:59 +00:00
2023-02-03 19:22:21 +00:00
final String addressString;
2024-05-15 21:20:45 +00:00
final CryptoCurrency coin;
2023-02-02 21:37:59 +00:00
final ClipboardInterface clipboard;
@override
State<AddressQrPopup> createState() => _AddressQrPopupState();
}
class _AddressQrPopupState extends State<AddressQrPopup> {
final _qrKey = GlobalKey();
final isDesktop = Util.isDesktop;
Future<void> _capturePng(bool shouldSaveInsteadOfShare) async {
try {
2024-05-15 21:20:45 +00:00
final RenderRepaintBoundary boundary =
2023-02-02 21:37:59 +00:00
_qrKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
2024-05-15 21:20:45 +00:00
final ui.Image image = await boundary.toImage();
final ByteData? byteData =
2023-02-02 21:37:59 +00:00
await image.toByteData(format: ui.ImageByteFormat.png);
2024-05-15 21:20:45 +00:00
final Uint8List pngBytes = byteData!.buffer.asUint8List();
2023-02-02 21:37:59 +00:00
if (shouldSaveInsteadOfShare) {
if (isDesktop) {
final dir = Directory("${Platform.environment['HOME']}");
if (!dir.existsSync()) {
throw Exception(
2024-05-27 23:56:22 +00:00
"Home dir not found while trying to open filepicker on QR image save",
);
2023-02-02 21:37:59 +00:00
}
final path = await FilePicker.platform.saveFile(
fileName: "qrcode.png",
initialDirectory: dir.path,
);
if (path != null) {
final file = File(path);
if (file.existsSync()) {
unawaited(
showFloatingFlushBar(
type: FlushBarType.warning,
message: "$path already exists!",
context: context,
),
);
} else {
await file.writeAsBytes(pngBytes);
unawaited(
showFloatingFlushBar(
type: FlushBarType.success,
message: "$path saved!",
context: context,
),
);
}
}
} else {
// await DocumentFileSavePlus.saveFile(
// pngBytes,
// "receive_qr_code_${DateTime.now().toLocal().toIso8601String()}.png",
// "image/png");
}
} else {
final tempDir = await getTemporaryDirectory();
final file = await File("${tempDir.path}/qrcode.png").create();
await file.writeAsBytes(pngBytes);
2024-05-27 23:56:22 +00:00
await Share.shareFiles(
["${tempDir.path}/qrcode.png"],
text: "Receive URI QR Code",
);
2023-02-02 21:37:59 +00:00
}
} catch (e) {
//todo: comeback to this
debugPrint(e.toString());
}
}
@override
Widget build(BuildContext context) {
return StackDialogBase(
child: Column(
children: [
Text(
"todo: custom label",
style: STextStyles.pageTitleH2(context),
),
const SizedBox(
height: 8,
),
Text(
2023-02-03 19:22:21 +00:00
widget.addressString,
2023-02-02 21:37:59 +00:00
style: STextStyles.itemSubtitle(context),
),
const SizedBox(
height: 16,
),
Center(
child: RepaintBoundary(
key: _qrKey,
2023-06-05 13:40:56 +00:00
child: QrImageView(
2023-02-02 21:37:59 +00:00
data: AddressUtils.buildUriString(
widget.coin,
2023-02-03 19:22:21 +00:00
widget.addressString,
2023-02-02 21:37:59 +00:00
{},
),
size: 220,
backgroundColor:
Theme.of(context).extension<StackColors>()!.popupBG,
foregroundColor:
Theme.of(context).extension<StackColors>()!.accentColorDark,
),
),
),
const SizedBox(
height: 16,
),
Row(
children: [
Expanded(
child: SecondaryButton(
width: 170,
buttonHeight: isDesktop ? ButtonHeight.l : null,
onPressed: () async {
await _capturePng(false);
},
label: "Share",
icon: SvgPicture.asset(
Assets.svg.share,
width: 20,
height: 20,
color: Theme.of(context)
.extension<StackColors>()!
.buttonTextSecondary,
),
),
),
const SizedBox(
width: 16,
),
Expanded(
child: PrimaryButton(
width: 170,
onPressed: () async {
await _capturePng(true);
},
label: "Save",
icon: SvgPicture.asset(
Assets.svg.arrowDown,
width: 20,
height: 20,
color: Theme.of(context)
.extension<StackColors>()!
.buttonTextPrimary,
),
),
),
],
2024-05-27 23:56:22 +00:00
),
2023-02-02 21:37:59 +00:00
],
),
);
}
}