You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

125 lines
3.6 KiB

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
// import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:flutter_qrcode_scanner/flutter_qrcode_scanner.dart';
class QRCodeScanner extends StatefulWidget {
const QRCodeScanner({
Key key,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _QRCodeScannerState();
}
class _QRCodeScannerState extends State<QRCodeScanner> {
var result;
String resultingCode = "";
QRViewController controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
// 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();
}
controller.resumeCamera();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
_buildQrView(context),
Container(
margin: EdgeInsets.only(
top: (MediaQuery.of(context).size.height) -
(MediaQuery.of(context).size.height * 0.935),
left: 10),
child: Material(
elevation: 5,
color: Color.fromRGBO(0, 0, 0, 0.4),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(25),
bottomRight: Radius.circular(25),
topLeft: Radius.circular(25),
topRight: Radius.circular(25),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child: IconButton(
icon: Icon(
Icons.arrow_back_ios,
size: 20,
),
color: Colors.white,
onPressed: () => Navigator.pop(context),
),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).size.height * 0.18,
),
child: Text(
"Scan QR Code from vender to redeem",
style: TextStyle(
color: Colors.grey,
),
),
),
),
],
),
);
}
Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 150.0
: 300.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea),
);
}
void _onQRViewCreated(QRViewController controller) {
setState(() {
this.controller = controller;
});
controller.scannedDataStream.listen((scanData) {
setState(() {
resultingCode = scanData;
});
if (resultingCode.isNotEmpty) {
this.controller.dispose();
Navigator.pop(context, resultingCode);
}
});
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}