Skip to content

Commit

Permalink
Change the QR Code implementation (#36)
Browse files Browse the repository at this point in the history
* chore: change qr implementation

* i am the best :) (#37)

* test qr embeded image implementation

* fix: missing comma

* fuck yes

* remove redundant code

* fix: solve the share and embedimage issue

---------

Co-authored-by: Jatin <main.jatink@gmail.com>
Co-authored-by: ShivamBansal07 <sb2368@srmist.edu.in>
  • Loading branch information
3 people authored Jan 21, 2024
1 parent ead8050 commit 18c3880
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 33 deletions.
Binary file added assets/kz_logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 36 additions & 33 deletions lib/components/qr_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kzlinks/utils/fetcher.dart';
import 'package:flutter/services.dart';
import 'package:kzlinks/utils/load_image.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:share_plus/share_plus.dart';

Future<void> showQRDialog(BuildContext context, String shortCode) async {
final kzillaUrl = 'https://kzilla.xyz/$shortCode';
final imageUrl =
'https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=$kzillaUrl';

Future<ByteData> shareQRImage() async {
final kzlogo = await loadImage(context, "assets/kz_logo.jpg");
final image = await QrPainter(
data: kzillaUrl,
version: QrVersions.auto,
errorCorrectionLevel: QrErrorCorrectLevel.H,
color: Colors.black,
emptyColor: Colors.white,
gapless: true,
embeddedImage: kzlogo,
).toImageData(1024.0);

return image!;
}

await showDialog(
context: context,
Expand Down Expand Up @@ -46,22 +62,13 @@ Future<void> showQRDialog(BuildContext context, String shortCode) async {
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: CachedNetworkImage(
imageUrl: imageUrl,
height: 250,
width: 250,
placeholder: (context, url) => const Center(
child: Text(
'Loading QR Code...',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
errorWidget: (context, url, error) => const Center(
child: Icon(Icons.error),
),
child: QrImageView(
data: kzillaUrl,
size: 200,
version: QrVersions.auto,
gapless: true,
errorCorrectionLevel: QrErrorCorrectLevel.H,
embeddedImage: AssetImage("assets/kz_logo.jpg"),
),
),
),
Expand All @@ -73,20 +80,16 @@ Future<void> showQRDialog(BuildContext context, String shortCode) async {
child: FilledButton(
onPressed: () async {
try {
final response = await dio.get(
imageUrl,
options: Options(responseType: ResponseType.bytes),
);
final logo = await shareQRImage();
final dir = await getTemporaryDirectory();
final byte = logo!.buffer.asUint8List();
final file = File("${dir.path}/Image.png");
await file.writeAsBytes(byte);
await Share.shareXFiles(
[
XFile.fromData(
response.data as Uint8List,
name: 'qr_code_$shortCode.jpeg',
mimeType: 'image/jpeg',
),
],
[XFile(file.path)],
text:
'QR code for $kzillaUrl generated with ❤️ by SRMKZILLA',
'QR code for ${kzillaUrl} generated with ❤️ by SRMKZILLA',
subject: 'QR Code',
);
} catch (e) {
debugPrint(e.toString());
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/load_image.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:async';
import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:flutter/material.dart';

Future<ui.Image> loadImage(BuildContext context, String assetPath) async {
final ByteData data = await DefaultAssetBundle.of(context).load(assetPath);
final Completer<ui.Image> completer = Completer();
ui.decodeImageFromList(data.buffer.asUint8List(), (ui.Image img) {
return completer.complete(img);
});
return completer.future;
}
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.7.3"
qr:
dependency: transitive
description:
name: qr
sha256: "64957a3930367bf97cc211a5af99551d630f2f4625e38af10edd6b19131b64b3"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
qr_flutter:
dependency: "direct main"
description:
name: qr_flutter
sha256: "5095f0fc6e3f71d08adef8feccc8cea4f12eec18a2e31c2e8d82cb6019f4b097"
url: "https://pub.dev"
source: hosted
version: "4.1.0"
rxdart:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
share_plus: ^7.1.0
jpeg_encode: ^1.0.1
cached_network_image: ^3.3.1
qr_flutter: ^4.1.0

dev_dependencies:

Expand Down

0 comments on commit 18c3880

Please sign in to comment.