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.
 
 
 
 
 
 

159 lignes
5.1 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:qr_code_scanner/qr_code_scanner.dart';
  3. void main() => runApp(MaterialApp(home: QRViewExample()));
  4. const flashOn = 'FLASH ON';
  5. const flashOff = 'FLASH OFF';
  6. const frontCamera = 'FRONT CAMERA';
  7. const backCamera = 'BACK CAMERA';
  8. class QRViewExample extends StatefulWidget {
  9. const QRViewExample({
  10. Key key,
  11. }) : super(key: key);
  12. @override
  13. State<StatefulWidget> createState() => _QRViewExampleState();
  14. }
  15. class _QRViewExampleState extends State<QRViewExample> {
  16. var qrText = '';
  17. var flashState = flashOn;
  18. var cameraState = frontCamera;
  19. QRViewController controller;
  20. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  21. @override
  22. Widget build(BuildContext context) {
  23. return Scaffold(
  24. body: Column(
  25. children: <Widget>[
  26. Expanded(
  27. flex: 4,
  28. child: QRView(
  29. key: qrKey,
  30. onQRViewCreated: _onQRViewCreated,
  31. overlay: QrScannerOverlayShape(
  32. borderColor: Colors.red,
  33. borderRadius: 10,
  34. borderLength: 30,
  35. borderWidth: 10,
  36. cutOutSize: 300,
  37. cutOutBottomOffset: 24,
  38. ),
  39. ),
  40. ),
  41. Expanded(
  42. flex: 1,
  43. child: FittedBox(
  44. fit: BoxFit.contain,
  45. child: Column(
  46. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  47. children: <Widget>[
  48. Text('This is the result of scan: $qrText'),
  49. Row(
  50. mainAxisAlignment: MainAxisAlignment.center,
  51. crossAxisAlignment: CrossAxisAlignment.center,
  52. children: <Widget>[
  53. Container(
  54. margin: EdgeInsets.all(8),
  55. child: RaisedButton(
  56. onPressed: () {
  57. if (controller != null) {
  58. controller.toggleFlash();
  59. if (_isFlashOn(flashState)) {
  60. setState(() {
  61. flashState = flashOff;
  62. });
  63. } else {
  64. setState(() {
  65. flashState = flashOn;
  66. });
  67. }
  68. }
  69. },
  70. child:
  71. Text(flashState, style: TextStyle(fontSize: 20)),
  72. ),
  73. ),
  74. Container(
  75. margin: EdgeInsets.all(8),
  76. child: RaisedButton(
  77. onPressed: () {
  78. if (controller != null) {
  79. controller.flipCamera();
  80. if (_isBackCamera(cameraState)) {
  81. setState(() {
  82. cameraState = frontCamera;
  83. });
  84. } else {
  85. setState(() {
  86. cameraState = backCamera;
  87. });
  88. }
  89. }
  90. },
  91. child:
  92. Text(cameraState, style: TextStyle(fontSize: 20)),
  93. ),
  94. )
  95. ],
  96. ),
  97. Row(
  98. mainAxisAlignment: MainAxisAlignment.center,
  99. crossAxisAlignment: CrossAxisAlignment.center,
  100. children: <Widget>[
  101. Container(
  102. margin: EdgeInsets.all(8),
  103. child: RaisedButton(
  104. onPressed: () {
  105. controller?.pauseCamera();
  106. },
  107. child: Text('pause', style: TextStyle(fontSize: 20)),
  108. ),
  109. ),
  110. Container(
  111. margin: EdgeInsets.all(8),
  112. child: RaisedButton(
  113. onPressed: () {
  114. controller?.resumeCamera();
  115. },
  116. child: Text('resume', style: TextStyle(fontSize: 20)),
  117. ),
  118. )
  119. ],
  120. ),
  121. ],
  122. ),
  123. ),
  124. )
  125. ],
  126. ),
  127. );
  128. }
  129. bool _isFlashOn(String current) {
  130. return flashOn == current;
  131. }
  132. bool _isBackCamera(String current) {
  133. return backCamera == current;
  134. }
  135. void _onQRViewCreated(QRViewController controller) {
  136. this.controller = controller;
  137. controller.scannedDataStream.listen((scanData) {
  138. setState(() {
  139. qrText = scanData;
  140. });
  141. });
  142. }
  143. @override
  144. void dispose() {
  145. controller.dispose();
  146. super.dispose();
  147. }
  148. }