import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
class QRScannerPage extends StatefulWidget {
const QRScannerPage({super.key});
@override
_QRScannerPageState createState() => _QRScannerPageState();
}
class _QRScannerPageState extends State<QRScannerPage> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
QRViewController? controller;
Barcode? result;
@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
controller!.pauseCamera();
}
controller!.resumeCamera();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.white,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: MediaQuery.of(context).size.width * 0.75,
),
),
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 0.1, sigmaY: 0.1),
child: Container(
color: Colors.black.withOpacity(0.3),
),
),
const Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 100.0),
child: Text(
"Scan QR code Aset BMD",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
const Align(
alignment: Alignment.center,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 500),
Text(
"Letakan code QR tepat di dalam kotak",
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold),
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 40.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.transparent,
shape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text(
"Batalkan",
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
),
),
),
],
),
);
}
void _onQRViewCreated(QRViewController controller) {
setState(() {
this.controller = controller;
});
controller.scannedDataStream.listen((scanData) {
setState(() {
result = scanData;
});
if (result != null) {
controller.pauseCamera();
_showResultDialog(context, result!.code);
}
});
}
void _showResultDialog(BuildContext context, String? code) {
showDialog(
context: context,
barrierColor: Colors.black.withOpacity(0.5),
builder: (_) => AlertDialog(
title: const Text('QR Code Detected'),
content: Text('Result: $code'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
controller?.resumeCamera();
},
child: const Text('OK'),
),
],
),
);
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}