The QR Code Scanner package for Appeto SDK.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

186 lignes
6.0 KiB

  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:qr_code_scanner/qr_code_scanner.dart';
  5. void main() => runApp(MaterialApp(home: QRViewExample()));
  6. const flashOn = 'FLASH ON';
  7. const flashOff = 'FLASH OFF';
  8. const frontCamera = 'FRONT CAMERA';
  9. const backCamera = 'BACK CAMERA';
  10. class QRViewExample extends StatefulWidget {
  11. const QRViewExample({
  12. Key key,
  13. }) : super(key: key);
  14. @override
  15. State<StatefulWidget> createState() => _QRViewExampleState();
  16. }
  17. class _QRViewExampleState extends State<QRViewExample> {
  18. Barcode result;
  19. var flashState = flashOn;
  20. var cameraState = frontCamera;
  21. QRViewController controller;
  22. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  23. // In order to get hot reload to work we need to pause the camera if the platform
  24. // is android, or resume the camera if the platform is iOS.
  25. @override
  26. void reassemble() {
  27. super.reassemble();
  28. if (Platform.isAndroid) {
  29. controller.pauseCamera();
  30. } else if (Platform.isIOS) {
  31. controller.resumeCamera();
  32. }
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. body: Column(
  38. children: <Widget>[
  39. Expanded(flex: 4, child: _buildQrView(context)),
  40. Expanded(
  41. flex: 1,
  42. child: FittedBox(
  43. fit: BoxFit.contain,
  44. child: Column(
  45. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  46. children: <Widget>[
  47. if (result != null)
  48. Text(
  49. 'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
  50. else
  51. Text('Scan a code'),
  52. Row(
  53. mainAxisAlignment: MainAxisAlignment.center,
  54. crossAxisAlignment: CrossAxisAlignment.center,
  55. children: <Widget>[
  56. Container(
  57. margin: EdgeInsets.all(8),
  58. child: RaisedButton(
  59. onPressed: () {
  60. if (controller != null) {
  61. controller.toggleFlash();
  62. if (_isFlashOn(flashState)) {
  63. setState(() {
  64. flashState = flashOff;
  65. });
  66. } else {
  67. setState(() {
  68. flashState = flashOn;
  69. });
  70. }
  71. }
  72. },
  73. child:
  74. Text(flashState, style: TextStyle(fontSize: 20)),
  75. ),
  76. ),
  77. Container(
  78. margin: EdgeInsets.all(8),
  79. child: RaisedButton(
  80. onPressed: () {
  81. if (controller != null) {
  82. controller.flipCamera();
  83. if (_isBackCamera(cameraState)) {
  84. setState(() {
  85. cameraState = frontCamera;
  86. });
  87. } else {
  88. setState(() {
  89. cameraState = backCamera;
  90. });
  91. }
  92. }
  93. },
  94. child:
  95. Text(cameraState, style: TextStyle(fontSize: 20)),
  96. ),
  97. )
  98. ],
  99. ),
  100. Row(
  101. mainAxisAlignment: MainAxisAlignment.center,
  102. crossAxisAlignment: CrossAxisAlignment.center,
  103. children: <Widget>[
  104. Container(
  105. margin: EdgeInsets.all(8),
  106. child: RaisedButton(
  107. onPressed: () {
  108. controller?.pauseCamera();
  109. },
  110. child: Text('pause', style: TextStyle(fontSize: 20)),
  111. ),
  112. ),
  113. Container(
  114. margin: EdgeInsets.all(8),
  115. child: RaisedButton(
  116. onPressed: () {
  117. controller?.resumeCamera();
  118. },
  119. child: Text('resume', style: TextStyle(fontSize: 20)),
  120. ),
  121. )
  122. ],
  123. ),
  124. ],
  125. ),
  126. ),
  127. )
  128. ],
  129. ),
  130. );
  131. }
  132. bool _isFlashOn(String current) {
  133. return flashOn == current;
  134. }
  135. bool _isBackCamera(String current) {
  136. return backCamera == current;
  137. }
  138. Widget _buildQrView(BuildContext context) {
  139. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  140. var scanArea = (MediaQuery.of(context).size.width < 400 ||
  141. MediaQuery.of(context).size.height < 400)
  142. ? 150.0
  143. : 300.0;
  144. // To ensure the Scanner view is properly sizes after rotation
  145. // we need to listen for Flutter SizeChanged notification and update controller
  146. return QRView(
  147. key: qrKey,
  148. cameraFacing: CameraFacing.front,
  149. onQRViewCreated: _onQRViewCreated,
  150. overlay: QrScannerOverlayShape(
  151. borderColor: Colors.red,
  152. borderRadius: 10,
  153. borderLength: 30,
  154. borderWidth: 10,
  155. cutOutSize: scanArea,
  156. ),
  157. );
  158. }
  159. void _onQRViewCreated(QRViewController controller) {
  160. this.controller = controller;
  161. controller.scannedDataStream.listen((scanData) {
  162. setState(() {
  163. result = scanData;
  164. });
  165. });
  166. }
  167. @override
  168. void dispose() {
  169. controller.dispose();
  170. super.dispose();
  171. }
  172. }