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.
 
 
 
 
 
 

174 lignes
5.6 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. void reassemble() {
  23. super.reassemble();
  24. controller.pauseCamera();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. body: Column(
  30. children: <Widget>[
  31. Expanded(flex: 4, child: _buildQrView(context)),
  32. Expanded(
  33. flex: 1,
  34. child: FittedBox(
  35. fit: BoxFit.contain,
  36. child: Column(
  37. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  38. children: <Widget>[
  39. Text('This is the result of scan: $qrText'),
  40. Row(
  41. mainAxisAlignment: MainAxisAlignment.center,
  42. crossAxisAlignment: CrossAxisAlignment.center,
  43. children: <Widget>[
  44. Container(
  45. margin: EdgeInsets.all(8),
  46. child: RaisedButton(
  47. onPressed: () {
  48. if (controller != null) {
  49. controller.toggleFlash();
  50. if (_isFlashOn(flashState)) {
  51. setState(() {
  52. flashState = flashOff;
  53. });
  54. } else {
  55. setState(() {
  56. flashState = flashOn;
  57. });
  58. }
  59. }
  60. },
  61. child:
  62. Text(flashState, style: TextStyle(fontSize: 20)),
  63. ),
  64. ),
  65. Container(
  66. margin: EdgeInsets.all(8),
  67. child: RaisedButton(
  68. onPressed: () {
  69. if (controller != null) {
  70. controller.flipCamera();
  71. if (_isBackCamera(cameraState)) {
  72. setState(() {
  73. cameraState = frontCamera;
  74. });
  75. } else {
  76. setState(() {
  77. cameraState = backCamera;
  78. });
  79. }
  80. }
  81. },
  82. child:
  83. Text(cameraState, style: TextStyle(fontSize: 20)),
  84. ),
  85. )
  86. ],
  87. ),
  88. Row(
  89. mainAxisAlignment: MainAxisAlignment.center,
  90. crossAxisAlignment: CrossAxisAlignment.center,
  91. children: <Widget>[
  92. Container(
  93. margin: EdgeInsets.all(8),
  94. child: RaisedButton(
  95. onPressed: () {
  96. controller?.pauseCamera();
  97. },
  98. child: Text('pause', style: TextStyle(fontSize: 20)),
  99. ),
  100. ),
  101. Container(
  102. margin: EdgeInsets.all(8),
  103. child: RaisedButton(
  104. onPressed: () {
  105. controller?.resumeCamera();
  106. },
  107. child: Text('resume', style: TextStyle(fontSize: 20)),
  108. ),
  109. )
  110. ],
  111. ),
  112. ],
  113. ),
  114. ),
  115. )
  116. ],
  117. ),
  118. );
  119. }
  120. bool _isFlashOn(String current) {
  121. return flashOn == current;
  122. }
  123. bool _isBackCamera(String current) {
  124. return backCamera == current;
  125. }
  126. Widget _buildQrView(BuildContext context) {
  127. // To ensure the Scanner view is properly sizes after rotation
  128. // we need to listen for Flutter SizeChanged notification and update controller
  129. return NotificationListener<SizeChangedLayoutNotification>(
  130. onNotification: (notification) {
  131. Future.microtask(() => controller?.updateDimensions(qrKey));
  132. return false;
  133. },
  134. child: SizeChangedLayoutNotifier(
  135. key: const Key('qr-size-notifier'),
  136. child: QRView(
  137. key: qrKey,
  138. onQRViewCreated: _onQRViewCreated,
  139. overlay: QrScannerOverlayShape(
  140. borderColor: Colors.red,
  141. borderRadius: 10,
  142. borderLength: 30,
  143. borderWidth: 10,
  144. cutOutSize: 300,
  145. ),
  146. )));
  147. }
  148. void _onQRViewCreated(QRViewController controller) {
  149. this.controller = controller;
  150. controller.scannedDataStream.listen((scanData) {
  151. setState(() {
  152. qrText = scanData;
  153. });
  154. });
  155. }
  156. @override
  157. void dispose() {
  158. controller.dispose();
  159. super.dispose();
  160. }
  161. }