Skip to content

Commit

Permalink
chore: change qr implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shailantani committed Jan 21, 2024
1 parent ead8050 commit 7ece442
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 47 deletions.
91 changes: 44 additions & 47 deletions lib/components/qr_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:kzlinks/utils/fetcher.dart';
import 'package:share_plus/share_plus.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:path_provider/path_provider.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';
// final imageUrl =
// 'https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=$kzillaUrl';

Future _shareQRImage() async {
final image = await QrPainter(
data: kzillaUrl,
version: QrVersions.auto,
gapless: true,
color: Colors.black,
emptyColor: Colors.white,
).toImageData(200.0); // Generate QR code image data

final filename = 'qr_code.png';
final tempDir =
await getTemporaryDirectory(); // Get temporary directory to store the generated image
final file = await File('${tempDir.path}/$filename')
.create(); // Create a file to store the generated image
var bytes = image!.buffer.asUint8List(); // Get the image bytes
await file.writeAsBytes(bytes); // Write the image bytes to the file
final path = await Share.shareFiles([file.path],
text: 'QR code for ${kzillaUrl} generated with ❤️ by SRMKZILLA',
subject: 'QR Code',
mimeTypes: [
'image/png'
]); // Share the generated image using the share_plus package
//print('QR code shared to: $path');
}

await showDialog(
context: context,
Expand Down Expand Up @@ -37,34 +61,21 @@ Future<void> showQRDialog(BuildContext context, String shortCode) async {
),
const SizedBox(height: 20),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: Colors.grey.shade300,
width: 2,
),
),
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),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: Colors.grey.shade300,
width: 2,
),
),
),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: QrImageView(
data: kzillaUrl,
version: QrVersions.auto,
size: 250,
),
)),
const SizedBox(height: 20),
const SizedBox(width: 10),
SizedBox(
Expand All @@ -73,21 +84,7 @@ Future<void> showQRDialog(BuildContext context, String shortCode) async {
child: FilledButton(
onPressed: () async {
try {
final response = await dio.get(
imageUrl,
options: Options(responseType: ResponseType.bytes),
);
await Share.shareXFiles(
[
XFile.fromData(
response.data as Uint8List,
name: 'qr_code_$shortCode.jpeg',
mimeType: 'image/jpeg',
),
],
text:
'QR code for $kzillaUrl generated with ❤️ by SRMKZILLA',
);
_shareQRImage();
} catch (e) {
debugPrint(e.toString());
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
24 changes: 24 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 All @@ -432,6 +448,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.27.7"
screenshot:
dependency: "direct main"
description:
name: screenshot
sha256: "455284ff1f5b911d94a43c25e1385485cf6b4f288293eba68f15dad711c7b81c"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
share_plus:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies:
share_plus: ^7.1.0
jpeg_encode: ^1.0.1
cached_network_image: ^3.3.1
qr_flutter: ^4.1.0
screenshot: ^2.1.0

dev_dependencies:

Expand Down

0 comments on commit 7ece442

Please sign in to comment.