Skip to content

Commit

Permalink
Merge pull request #8 from sankethsj/ui-change
Browse files Browse the repository at this point in the history
updated Scanner UI
sankethsj authored Jun 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 6723ee7 + c4c0bac commit 95032af
Showing 5 changed files with 137 additions and 261 deletions.
205 changes: 131 additions & 74 deletions lib/pages/qr_scanner.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:qr_code_gen/pages/scan_image.dart';
import 'package:qr_code_gen/pages/scan_via_camera.dart';
import 'dart:io';
import 'package:qr_code_gen/pages/scan_result.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

class QrScanner extends StatefulWidget {
const QrScanner({Key? key}) : super(key: key);
@@ -10,87 +12,142 @@ class QrScanner extends StatefulWidget {
}

class QrScannerState extends State<QrScanner> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
Barcode? result;
QRViewController? controller;

// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
controller!.pauseCamera();
} else if (Platform.isIOS) {
controller!.resumeCamera();
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).cardColor,
border: Border.all(color: Theme.of(context).dividerColor),
borderRadius: const BorderRadius.all(Radius.circular(20.0)),
),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
Icons.camera_alt_outlined,
size: 120,
weight: 5,
body: Column(
children: <Widget>[
Expanded(
flex: 5,
child: _buildQrView(context),
),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
const Text('Point your Camera towards a QR code'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () async {
await controller?.toggleFlash();
setState(() {});
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
FutureBuilder(
future: controller?.getFlashStatus(),
builder: (context, snapshot) {
if (snapshot.data == true) {
return const Icon(
Icons.flashlight_on_rounded);
} else {
return const Icon(
Icons.flashlight_off_rounded);
}
},
),
],
),
),
const SizedBox(height: 20),
const Text("Scan QR codes directly from your camera"),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CameraScanner()),
);
),
Container(
margin: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () async {
await controller?.flipCamera();
setState(() {});
},
child: const Text("Open Camera"),
child: const Icon(Icons.cameraswitch_rounded),
),
],
),
),
),
const SizedBox(height: 20),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).cardColor,
border: Border.all(color: Theme.of(context).dividerColor),
borderRadius:
const BorderRadius.all(Radius.circular(20.0)),
),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
Icons.photo_library_outlined,
size: 120,
weight: 5,
),
const SizedBox(height: 20),
const Text("Scan QR codes from images in your gallery"),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ScanImage()),
);
},
child: const Text("Select Image"),
),
],
)),
),
],
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ScanImage()),
);
},
child: const Text("Select Image"),
),
],
),
],
),
)
],
),
);
}

Widget _buildQrView(BuildContext context) {
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 280.0
: 400.0;

return QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Theme.of(context).primaryColor,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea),
onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
);
}

void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
if (!p) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Camera Permission denied')),
);
}
}

void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
controller.pauseCamera();

setState(() {
result = scanData;
});

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ScanResult(
resultFormat: result!.format,
resultText: result!.code ?? "No data found!",
),
),
).then((value) => controller.resumeCamera());
});
}
}
10 changes: 5 additions & 5 deletions lib/pages/scan_image.dart
Original file line number Diff line number Diff line change
@@ -20,14 +20,14 @@ class ScanImageState extends State<ScanImage> {
@override
void initState() {
super.initState();
pickImage();
pickImage(context);
}

Future pickImage() async {
Future pickImage(context) async {
try {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) {
Navigator.pop(context);
Navigator.of(context).pop();
return;
}
final imageTemp = File(image.path);
@@ -149,7 +149,7 @@ class ScanImageState extends State<ScanImage> {
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: pickImage,
onPressed: ()=> pickImage(context),
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
@@ -164,7 +164,7 @@ class ScanImageState extends State<ScanImage> {
SizedBox(
width: double.infinity,
child: FilledButton(
onPressed: pickImage,
onPressed: () => pickImage(context),
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
2 changes: 0 additions & 2 deletions lib/pages/scan_result.dart
Original file line number Diff line number Diff line change
@@ -117,8 +117,6 @@ class ScanResultState extends State<ScanResult> {
String url = "";
List links = findUrls(resultText);

print("links found in text are");
print(links);
bool resultContainsLink = links.isNotEmpty;
if (resultContainsLink) {
url = links[0];
179 changes: 0 additions & 179 deletions lib/pages/scan_via_camera.dart

This file was deleted.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ description: A Material You themed QRCode generator - Create, Save, Scan QR code
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.2.1+1703.2024
version: 1.2.2+2024.0622

environment:
sdk: '>=2.19.6 <3.0.0'

0 comments on commit 95032af

Please sign in to comment.