printQRCodeToTerminal static method

void printQRCodeToTerminal(
  1. String data, {
  2. int size = 4,
})

Prints a QR code for the given data to the terminal as ASCII size controls the QR version (1-10, default 4, smaller = smaller QR)

Implementation

static void printQRCodeToTerminal(String data, {int size = 4}) {
  try {
    final typeNumber = size.clamp(1, 10);
    final qrCode = QrCode(typeNumber, QrErrorCorrectLevel.L)..addData(data);
    final qrImage = QrImage(qrCode);
    final int moduleCount = qrImage.moduleCount;

    print('\nπŸ“± QR Code for: $data');
    print('β”Œ${'─' * (moduleCount * 2)}┐');

    for (int y = 0; y < moduleCount; y++) {
      final buffer = StringBuffer('β”‚');
      for (int x = 0; x < moduleCount; x++) {
        buffer.write(qrImage.isDark(y, x) ? 'β–ˆβ–ˆ' : '  ');
      }
      buffer.write('β”‚');
      print(buffer.toString());
    }

    print('β””${'─' * (moduleCount * 2)}β”˜');
    print('πŸ“± Scan this QR code with your mobile device');
  } catch (e) {
    print('❌ Failed to generate QR code for terminal: $e');
  }
}