The QR Code Scanner package for Appeto SDK.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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