generateQrCodeAndLink static method

Future<void> generateQrCodeAndLink({
  1. String? url,
  2. bool? generateLink = true,
})

Generate a QR code and displays & save it. Optionally, print a download link.

Default values for generateQr and generateLink are true.

Implementation

static Future<void> generateQrCodeAndLink({
  String? url,
  bool? generateLink = true,
}) async {
  final qrConfig = Config().config.qrCode;
  final isQrEnable = qrConfig.enabled;
  final isQrShowInConsole = qrConfig.showInCommand;
  final qrImgSize = qrConfig.size;
  final isQrSaveFile = qrConfig.saveFile;
  final qrImgSavePath = qrConfig.savePath;
  final qrCorrectionLevel = qrConfig.errorCorrectionLevel;

  // Directly accessing UploadState and updating it
  final uploadState = UploadState();

  final String? uploadLink = uploadState.uploadLink;

  if (isQrEnable && (uploadLink != null || url != null)) {
    final qrCode = QrCode.fromData(
      data: url ?? uploadLink.toString(),
      errorCorrectLevel: getQrCorrectionLevel(qrCorrectionLevel),
    );
    final qrImg = QrImage(qrCode);

    if (isQrShowInConsole) {
      printQrCode(qrImg);
    }

    // Create an empty image with the same dimensions as the QR code
    final qrImage =
        img.Image(width: qrCode.moduleCount, height: qrCode.moduleCount);

    // Loop through each module of the QR code and set the pixel
    for (int x = 0; x < qrCode.moduleCount; x++) {
      for (int y = 0; y < qrCode.moduleCount; y++) {
        // Set pixel to black (0xFF000000) for dark modules, white (0xFFFFFFFF) for light modules
        qrImage.setPixel(
          x,
          y,
          qrImg.isDark(y, x)
              ? img.ColorFloat16.rgb(255, 255, 255)
              : img.ColorFloat16.rgb(0, 0, 0),
        );
      }
    }
    if (isQrSaveFile) {
      final scaledImage = img.copyResize(
        qrImage,
        width: qrImgSize,
        height: qrImgSize,
      );
      final file = File(qrImgSavePath);
      file.writeAsBytesSync(img.encodePng(scaledImage));
      showHighlight(
        firstMessage: 'QR code saved to',
        highLightmessage: qrImgSavePath,
      );
    }
  }
  if (generateLink == true && (uploadLink != null || url != null)) {
    Helpers.showHighlight(
      firstMessage: '📥 Download the APK here:',
      highLightmessage: url ?? uploadLink,
    );
  }
}