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.
 
 
 
 
 
 

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