|
|
@@ -21,16 +21,14 @@ Demonstrates how to use the qr_code_scanner plugin. |
|
|
|
|
|
|
|
|
## Example: |
|
|
## Example: |
|
|
```dart |
|
|
```dart |
|
|
|
|
|
import 'dart:io'; |
|
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import 'package:qr_code_scanner/qr_code_scanner.dart'; |
|
|
import 'package:qr_code_scanner/qr_code_scanner.dart'; |
|
|
|
|
|
|
|
|
void main() => runApp(MaterialApp(home: QRViewExample())); |
|
|
void main() => runApp(MaterialApp(home: QRViewExample())); |
|
|
|
|
|
|
|
|
const flashOn = 'FLASH ON'; |
|
|
|
|
|
const flashOff = 'FLASH OFF'; |
|
|
|
|
|
const frontCamera = 'FRONT CAMERA'; |
|
|
|
|
|
const backCamera = 'BACK CAMERA'; |
|
|
|
|
|
|
|
|
|
|
|
class QRViewExample extends StatefulWidget { |
|
|
class QRViewExample extends StatefulWidget { |
|
|
const QRViewExample({ |
|
|
const QRViewExample({ |
|
|
Key key, |
|
|
Key key, |
|
|
@@ -42,8 +40,6 @@ class QRViewExample extends StatefulWidget { |
|
|
|
|
|
|
|
|
class _QRViewExampleState extends State<QRViewExample> { |
|
|
class _QRViewExampleState extends State<QRViewExample> { |
|
|
Barcode result; |
|
|
Barcode result; |
|
|
var flashState = flashOn; |
|
|
|
|
|
var cameraState = frontCamera; |
|
|
|
|
|
QRViewController controller; |
|
|
QRViewController controller; |
|
|
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); |
|
|
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); |
|
|
|
|
|
|
|
|
@@ -84,44 +80,33 @@ class _QRViewExampleState extends State<QRViewExample> { |
|
|
Container( |
|
|
Container( |
|
|
margin: EdgeInsets.all(8), |
|
|
margin: EdgeInsets.all(8), |
|
|
child: RaisedButton( |
|
|
child: RaisedButton( |
|
|
onPressed: () { |
|
|
|
|
|
if (controller != null) { |
|
|
|
|
|
controller.toggleFlash(); |
|
|
|
|
|
if (_isFlashOn(flashState)) { |
|
|
|
|
|
setState(() { |
|
|
|
|
|
flashState = flashOff; |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
setState(() { |
|
|
|
|
|
flashState = flashOn; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
child: |
|
|
|
|
|
Text(flashState, style: TextStyle(fontSize: 20)), |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
onPressed: () => setState(() { |
|
|
|
|
|
controller?.toggleFlash(); |
|
|
|
|
|
}), |
|
|
|
|
|
child: FutureBuilder( |
|
|
|
|
|
future: controller?.getFlashStatus(), |
|
|
|
|
|
builder: (context, snapshot) { |
|
|
|
|
|
return Text('Flash: ${snapshot.data}'); |
|
|
|
|
|
}, |
|
|
|
|
|
)), |
|
|
), |
|
|
), |
|
|
Container( |
|
|
Container( |
|
|
margin: EdgeInsets.all(8), |
|
|
margin: EdgeInsets.all(8), |
|
|
child: RaisedButton( |
|
|
child: RaisedButton( |
|
|
onPressed: () { |
|
|
|
|
|
if (controller != null) { |
|
|
|
|
|
controller.flipCamera(); |
|
|
|
|
|
if (_isBackCamera(cameraState)) { |
|
|
|
|
|
setState(() { |
|
|
|
|
|
cameraState = frontCamera; |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
setState(() { |
|
|
|
|
|
cameraState = backCamera; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
child: |
|
|
|
|
|
Text(cameraState, style: TextStyle(fontSize: 20)), |
|
|
|
|
|
), |
|
|
|
|
|
|
|
|
onPressed: () => setState(() { |
|
|
|
|
|
controller?.flipCamera(); |
|
|
|
|
|
}), |
|
|
|
|
|
child: FutureBuilder( |
|
|
|
|
|
future: controller?.getCameraInfo(), |
|
|
|
|
|
builder: (context, snapshot) { |
|
|
|
|
|
if (snapshot.data != null) { |
|
|
|
|
|
return Text( |
|
|
|
|
|
'Camera facing ${describeEnum(snapshot.data)}'); |
|
|
|
|
|
} else { |
|
|
|
|
|
return Text('loading'); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
)), |
|
|
) |
|
|
) |
|
|
], |
|
|
], |
|
|
), |
|
|
), |
|
|
@@ -158,14 +143,6 @@ class _QRViewExampleState extends State<QRViewExample> { |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool _isFlashOn(String current) { |
|
|
|
|
|
return flashOn == current; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool _isBackCamera(String current) { |
|
|
|
|
|
return backCamera == current; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Widget _buildQrView(BuildContext context) { |
|
|
Widget _buildQrView(BuildContext context) { |
|
|
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly. |
|
|
// 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 || |
|
|
var scanArea = (MediaQuery.of(context).size.width < 400 || |
|
|
@@ -174,29 +151,27 @@ class _QRViewExampleState extends State<QRViewExample> { |
|
|
: 300.0; |
|
|
: 300.0; |
|
|
// To ensure the Scanner view is properly sizes after rotation |
|
|
// To ensure the Scanner view is properly sizes after rotation |
|
|
// we need to listen for Flutter SizeChanged notification and update controller |
|
|
// we need to listen for Flutter SizeChanged notification and update controller |
|
|
return NotificationListener<SizeChangedLayoutNotification>( |
|
|
|
|
|
onNotification: (notification) { |
|
|
|
|
|
Future.microtask( |
|
|
|
|
|
() => controller?.updateDimensions(qrKey, scanArea: scanArea)); |
|
|
|
|
|
return false; |
|
|
|
|
|
}, |
|
|
|
|
|
child: SizeChangedLayoutNotifier( |
|
|
|
|
|
key: const Key('qr-size-notifier'), |
|
|
|
|
|
child: QRView( |
|
|
|
|
|
key: qrKey, |
|
|
|
|
|
onQRViewCreated: _onQRViewCreated, |
|
|
|
|
|
overlay: QrScannerOverlayShape( |
|
|
|
|
|
borderColor: Colors.red, |
|
|
|
|
|
borderRadius: 10, |
|
|
|
|
|
borderLength: 30, |
|
|
|
|
|
borderWidth: 10, |
|
|
|
|
|
cutOutSize: scanArea, |
|
|
|
|
|
), |
|
|
|
|
|
))); |
|
|
|
|
|
|
|
|
return QRView( |
|
|
|
|
|
key: qrKey, |
|
|
|
|
|
// You can choose between CameraFacing.front or CameraFacing.back. Defaults to CameraFacing.back |
|
|
|
|
|
cameraFacing: CameraFacing.front, |
|
|
|
|
|
onQRViewCreated: _onQRViewCreated, |
|
|
|
|
|
// Choose formats you want to scan. Defaults to all formats. |
|
|
|
|
|
formatsAllowed: [BarcodeFormat.qrcode], |
|
|
|
|
|
overlay: QrScannerOverlayShape( |
|
|
|
|
|
borderColor: Colors.red, |
|
|
|
|
|
borderRadius: 10, |
|
|
|
|
|
borderLength: 30, |
|
|
|
|
|
borderWidth: 10, |
|
|
|
|
|
cutOutSize: scanArea, |
|
|
|
|
|
), |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void _onQRViewCreated(QRViewController controller) { |
|
|
void _onQRViewCreated(QRViewController controller) { |
|
|
this.controller = controller; |
|
|
|
|
|
|
|
|
setState(() { |
|
|
|
|
|
this.controller = controller; |
|
|
|
|
|
}); |
|
|
controller.scannedDataStream.listen((scanData) { |
|
|
controller.scannedDataStream.listen((scanData) { |
|
|
setState(() { |
|
|
setState(() { |
|
|
result = scanData; |
|
|
result = scanData; |
|
|
|