Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix receive amount formatting and QrInputTooLongException #59

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class _ReceiveLargeCardState extends State<ReceiveLargeCard> {
),
ReceiveQrImage(
data: _getQrString(),
size: 150.0,
size: 150,
tokenStandard: _selectedToken.tokenStandard,
context: context,
),
Expand All @@ -120,7 +120,12 @@ class _ReceiveLargeCardState extends State<ReceiveLargeCard> {
autovalidateMode:
AutovalidateMode.onUserInteraction,
child: InputField(
validator: InputValidators.validateAmount,
validator: (value) =>
InputValidators.correctValue(
value,
kBigP255m1,
_selectedToken.decimals,
BigInt.zero),
onChanged: (value) => setState(() {}),
inputFormatters:
FormatUtils.getAmountTextInputFormatters(
Expand Down Expand Up @@ -180,8 +185,8 @@ class _ReceiveLargeCardState extends State<ReceiveLargeCard> {
}

String _getQrString() {
return '${_selectedToken.symbol.toLowerCase()}:$_selectedSelfAddress?zts='
'${_selectedToken.tokenStandard}'
return '${_selectedToken.symbol.toLowerCase()}:'
'$_selectedSelfAddress?zts=${_selectedToken.tokenStandard}'
'&amount=${_getAmount()}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class _ReceiveMediumCardState extends State<ReceiveMediumCard> {
),
ReceiveQrImage(
data: _getQrString(),
size: 110.0,
size: 110,
tokenStandard: _selectedToken.tokenStandard,
context: context,
),
Expand All @@ -123,7 +123,11 @@ class _ReceiveMediumCardState extends State<ReceiveMediumCard> {
key: _amountKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: InputField(
validator: InputValidators.validateAmount,
validator: (value) => InputValidators.correctValue(
value,
kBigP255m1,
_selectedToken.decimals,
BigInt.zero),
onChanged: (value) => setState(() {}),
inputFormatters:
FormatUtils.getAmountTextInputFormatters(
Expand Down
240 changes: 139 additions & 101 deletions lib/widgets/reusable_widgets/receive_qr_image.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
import 'package:lottie/lottie.dart';
import 'package:open_filex/open_filex.dart';
import 'package:path/path.dart' as path;
import 'package:pretty_qr_code/pretty_qr_code.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
import 'package:zenon_syrius_wallet_flutter/utils/color_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/utils.dart';
Expand All @@ -15,13 +16,16 @@ import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class ReceiveQrImage extends StatelessWidget {
final String data;
final double size;
final int size;
final TokenStandard tokenStandard;
final BuildContext context;

final ScreenshotController screenshotController = ScreenshotController();
static const decorationImage = PrettyQrDecorationImage(
image: AssetImage('assets/images/qr_code_child_image_znn.png'),
position: PrettyQrDecorationImagePosition.embedded,
);

ReceiveQrImage({
const ReceiveQrImage({
required this.data,
required this.size,
required this.tokenStandard,
Expand All @@ -31,128 +35,162 @@ class ReceiveQrImage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Screenshot(
controller: screenshotController,
child: ClipRRect(
borderRadius: BorderRadius.circular(
15.0,
),
child: Container(
padding: const EdgeInsets.all(
10.0,
),
color: Theme.of(context).colorScheme.background,
child: ContextMenuRegion(
contextMenuBuilder: (context, offset) {
return AdaptiveTextSelectionToolbar(
anchors: TextSelectionToolbarAnchors(
primaryAnchor: offset,
),
children: [
Row(
children: [
Expanded(
child: Directionality(
textDirection: TextDirection.rtl,
child: TextButton.icon(
icon: Icon(
MaterialCommunityIcons.share,
color:
Theme.of(context).colorScheme.onBackground,
size: 14,
),
onPressed: () {
ContextMenuController.removeAny();
_shareQR();
},
style: TextButton.styleFrom(
shape: const RoundedRectangleBorder(),
),
label: Text(
AdaptiveTextSelectionToolbar.getButtonLabel(
context,
ContextMenuButtonItem(
label: 'Share QR', onPressed: () {})),
style:
Theme.of(context).textTheme.bodyMedium),
return ClipRRect(
borderRadius: BorderRadius.circular(
15.0,
),
child: Container(
height: size + 20,
width: size + 20,
padding: const EdgeInsets.all(
10.0,
),
color: Theme.of(context).colorScheme.background,
child: ContextMenuRegion(
contextMenuBuilder: (context, offset) {
return AdaptiveTextSelectionToolbar(
anchors: TextSelectionToolbarAnchors(
primaryAnchor: offset,
),
children: [
Row(
children: [
Expanded(
child: Directionality(
textDirection: TextDirection.rtl,
child: TextButton.icon(
icon: Icon(
MaterialCommunityIcons.share,
color: Theme.of(context).colorScheme.onBackground,
size: 14,
),
onPressed: () {
ContextMenuController.removeAny();
_shareQR();
},
style: TextButton.styleFrom(
shape: const RoundedRectangleBorder(),
),
label: Text(
AdaptiveTextSelectionToolbar.getButtonLabel(
context,
ContextMenuButtonItem(
label: 'Share QR', onPressed: () {})),
style: Theme.of(context).textTheme.bodyMedium),
),
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
flex: 1,
child: Directionality(
textDirection: TextDirection.rtl,
child: TextButton.icon(
icon: Icon(
Icons.save_alt,
color:
Theme.of(context).colorScheme.onBackground,
size: 14,
),
onPressed: () {
ContextMenuController.removeAny();
_saveQR();
},
style: TextButton.styleFrom(
shape: const RoundedRectangleBorder(),
),
label: Text(
AdaptiveTextSelectionToolbar.getButtonLabel(
context,
ContextMenuButtonItem(
label: 'Save QR', onPressed: () {})),
style:
Theme.of(context).textTheme.bodyMedium),
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
flex: 1,
child: Directionality(
textDirection: TextDirection.rtl,
child: TextButton.icon(
icon: Icon(
Icons.save_alt,
color: Theme.of(context).colorScheme.onBackground,
size: 14,
),
onPressed: () {
ContextMenuController.removeAny();
_saveQR();
},
style: TextButton.styleFrom(
shape: const RoundedRectangleBorder(),
),
label: Text(
AdaptiveTextSelectionToolbar.getButtonLabel(
context,
ContextMenuButtonItem(
label: 'Save QR', onPressed: () {})),
style: Theme.of(context).textTheme.bodyMedium),
),
),
],
),
],
);
},
child: PrettyQr(
),
],
),
],
);
},
child: PrettyQrView.data(
data: data,
size: size,
elementColor: ColorUtils.getTokenColor(tokenStandard),
image: const AssetImage(
'assets/images/qr_code_child_image_znn.png'),
typeNumber: 7,
decoration: PrettyQrDecoration(
shape: PrettyQrSmoothSymbol(
roundFactor: 0,
color: ColorUtils.getTokenColor(tokenStandard),
),
image: decorationImage),
errorCorrectLevel: QrErrorCorrectLevel.M,
roundEdges: true,
),
errorBuilder: (context, error, stack) => Center(
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Lottie.asset(
'assets/lottie/ic_anim_no_data.json',
width: 32.0,
height: 32.0,
),
Tooltip(
message: error.toString(),
child: Text(
'Failed to create QR code',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyMedium,
)),
],
),
),
))),
),
);
}

Future<Uint8List?> _getQRImageData() async {
final qr = QrImage(QrCode.fromData(
data: data,
errorCorrectLevel: QrErrorCorrectLevel.M,
));

final b = await qr.toImageAsBytes(
size: size,
format: ImageByteFormat.png,
decoration: PrettyQrDecoration(
shape: PrettyQrSmoothSymbol(
roundFactor: 0,
color: ColorUtils.getTokenColor(tokenStandard),
),
),
));
image: decorationImage));

if (b != null) return b.buffer.asUint8List();
return null;
}

void _saveQR() async {
Uint8List? capture = await screenshotController.capture(
delay: const Duration(milliseconds: 20));
if (capture != null) {
final imageData = await _getQRImageData();
if (imageData != null) {
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
final imagePath = await File(
'${znnDefaultPaths.cache.path}${path.separator}$fileName.png')
.create();
await imagePath.writeAsBytes(capture);
await imagePath.writeAsBytes(imageData);
await OpenFilex.open(imagePath.path);
}
}

void _shareQR() async {
Uint8List? capture = await screenshotController.capture(
delay: const Duration(milliseconds: 20));
if (capture != null) {
final imageData = await _getQRImageData();
if (imageData != null) {
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
final imagePath = await File(
'${znnDefaultPaths.cache.path}${path.separator}$fileName.png')
.create();
await imagePath.writeAsBytes(capture);
await imagePath.writeAsBytes(imageData);
await Share.shareXFiles([XFile(imagePath.path)]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,10 @@ packages:
dependency: "direct main"
description:
name: pretty_qr_code
sha256: ea7ccb3069e0f5b89b441449b9ec10f4148ddda7a4bef89a130d2ebdaa0be647
sha256: "47a0fde3967e01ea31985d1a11a998fab1ab900becdba592e9abb0a4034b807e"
url: "https://pub.dev"
source: hosted
version: "2.0.3"
version: "3.2.1"
provider:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies:
share_plus: ^7.0.2
page_transition: ^2.0.4
file_selector: ^0.9.2+2
pretty_qr_code: ^2.0.3
pretty_qr_code: ^3.2.1
screenshot: ^2.1.0
desktop_drop: ^0.4.0
validators: ^3.0.0
Expand Down
Loading