| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/new-ui/widgets/coins_page/token_image_widget.dart'; |
| 3 | import 'package:cake_wallet/src/widgets/cake_image_widget.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:qr_flutter/qr_flutter.dart' as qr; |
| 6 | |
| 7 | class QrImage extends StatelessWidget { |
| 8 | QrImage({ |
| 9 | required this.data, |
| 10 | this.foregroundColor = Colors.black, |
| 11 | this.backgroundColor = Colors.white, |
| 12 | this.size = 100.0, |
| 13 | this.version, |
| 14 | this.errorCorrectionLevel = qr.QrErrorCorrectLevel.H, |
| 15 | this.embeddedImagePath, |
| 16 | this.badgeImageOnEmbeddedImagePath, |
| 17 | this.semanticsLabel, |
| 18 | }); |
| 19 | |
| 20 | final double? size; |
| 21 | final Color foregroundColor; |
| 22 | final Color backgroundColor; |
| 23 | final String data; |
| 24 | final int? version; |
| 25 | final int errorCorrectionLevel; |
| 26 | final String? embeddedImagePath; |
| 27 | final String? badgeImageOnEmbeddedImagePath; |
| 28 | |
| 29 | /// Accessible name announced for the code itself. Defaults to a generic |
| 30 | /// localized "QR code" so the code is always discoverable; pass a more |
| 31 | /// specific label when the host screen knows what the code encodes. |
| 32 | final String? semanticsLabel; |
| 33 | |
| 34 | @override |
| 35 | Widget build(BuildContext context) { |
| 36 | final imagePath = embeddedImagePath ?? 'assets/images/qr-cake.png'; |
| 37 | final qrSize = size ?? 100.0; |
| 38 | final logoSize = qrSize * 0.30; |
| 39 | |
| 40 | final centerImage = TokenImageWidget( |
| 41 | imageUrl: imagePath, |
| 42 | size: logoSize * 0.8, |
| 43 | ); |
| 44 | |
| 45 | Widget centerImageToUse; |
| 46 | if (badgeImageOnEmbeddedImagePath != null) { |
| 47 | centerImageToUse = Stack( |
| 48 | children: [ |
| 49 | centerImage, |
| 50 | Positioned( |
| 51 | bottom: 0, |
| 52 | right: 0, |
| 53 | child: Align( |
| 54 | alignment: Alignment.bottomRight, |
| 55 | child: CakeImageWidget( |
| 56 | imageUrl: badgeImageOnEmbeddedImagePath!, |
| 57 | width: 24, |
| 58 | height: 24, |
| 59 | ), |
| 60 | ), |
| 61 | ), |
| 62 | ], |
| 63 | ); |
| 64 | } else { |
| 65 | centerImageToUse = centerImage; |
| 66 | } |
| 67 | return Stack( |
| 68 | alignment: Alignment.center, |
| 69 | children: [ |
| 70 | qr.QrImageView( |
| 71 | data: data, |
| 72 | errorCorrectionLevel: errorCorrectionLevel, |
| 73 | version: version ?? qr.QrVersions.auto, |
| 74 | size: qrSize, |
| 75 | foregroundColor: foregroundColor, |
| 76 | backgroundColor: backgroundColor, |
| 77 | padding: const EdgeInsets.all(12.0), |
| 78 | // The package default is a hardcoded English "qr code"; use the |
| 79 | // localized string so the code is announced in the user's language. |
| 80 | semanticsLabel: semanticsLabel ?? S.of(context).scan_qr_code, |
| 81 | ), |
| 82 | // The embedded logo and its badge are purely decorative — the node |
| 83 | // above already announces the code — so keep them out of the tree. |
| 84 | ExcludeSemantics(child: centerImageToUse), |
| 85 | ], |
| 86 | ); |
| 87 | } |
| 88 | } |