The QR Code Scanner package for Appeto SDK.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

166 linhas
5.3 KiB

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