The QR Code Scanner package for Appeto SDK.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

193 Zeilen
6.4 KiB

  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:qr_code_scanner/qr_code_scanner.dart';
  5. void main() => runApp(MaterialApp(home: QRViewExample()));
  6. const flashOn = 'FLASH ON';
  7. const flashOff = 'FLASH OFF';
  8. const frontCamera = 'FRONT CAMERA';
  9. const backCamera = 'BACK CAMERA';
  10. class QRViewExample extends StatefulWidget {
  11. const QRViewExample({
  12. Key key,
  13. }) : super(key: key);
  14. @override
  15. State<StatefulWidget> createState() => _QRViewExampleState();
  16. }
  17. class _QRViewExampleState extends State<QRViewExample> {
  18. Barcode result;
  19. var flashState = flashOn;
  20. var cameraState = frontCamera;
  21. QRViewController controller;
  22. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  23. // In order to get hot reload to work we need to pause the camera if the platform
  24. // is android, or resume the camera if the platform is iOS.
  25. @override
  26. void reassemble() {
  27. super.reassemble();
  28. if (Platform.isAndroid) {
  29. controller.pauseCamera();
  30. } else if (Platform.isIOS) {
  31. controller.resumeCamera();
  32. }
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. body: Column(
  38. children: <Widget>[
  39. Expanded(flex: 4, child: _buildQrView(context)),
  40. Expanded(
  41. flex: 1,
  42. child: FittedBox(
  43. fit: BoxFit.contain,
  44. child: Column(
  45. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  46. children: <Widget>[
  47. if (result != null)
  48. Text(
  49. 'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
  50. else
  51. Text('Scan a code'),
  52. Row(
  53. mainAxisAlignment: MainAxisAlignment.center,
  54. crossAxisAlignment: CrossAxisAlignment.center,
  55. children: <Widget>[
  56. Container(
  57. margin: EdgeInsets.all(8),
  58. child: RaisedButton(
  59. onPressed: () {
  60. if (controller != null) {
  61. controller.toggleFlash();
  62. if (_isFlashOn(flashState)) {
  63. setState(() {
  64. flashState = flashOff;
  65. });
  66. } else {
  67. setState(() {
  68. flashState = flashOn;
  69. });
  70. }
  71. }
  72. },
  73. child:
  74. Text(flashState, style: TextStyle(fontSize: 20)),
  75. ),
  76. ),
  77. Container(
  78. margin: EdgeInsets.all(8),
  79. child: RaisedButton(
  80. onPressed: () {
  81. if (controller != null) {
  82. controller.flipCamera();
  83. if (_isBackCamera(cameraState)) {
  84. setState(() {
  85. cameraState = frontCamera;
  86. });
  87. } else {
  88. setState(() {
  89. cameraState = backCamera;
  90. });
  91. }
  92. }
  93. },
  94. child:
  95. Text(cameraState, style: TextStyle(fontSize: 20)),
  96. ),
  97. )
  98. ],
  99. ),
  100. Row(
  101. mainAxisAlignment: MainAxisAlignment.center,
  102. crossAxisAlignment: CrossAxisAlignment.center,
  103. children: <Widget>[
  104. Container(
  105. margin: EdgeInsets.all(8),
  106. child: RaisedButton(
  107. onPressed: () {
  108. controller?.pauseCamera();
  109. },
  110. child: Text('pause', style: TextStyle(fontSize: 20)),
  111. ),
  112. ),
  113. Container(
  114. margin: EdgeInsets.all(8),
  115. child: RaisedButton(
  116. onPressed: () {
  117. controller?.resumeCamera();
  118. },
  119. child: Text('resume', style: TextStyle(fontSize: 20)),
  120. ),
  121. )
  122. ],
  123. ),
  124. ],
  125. ),
  126. ),
  127. )
  128. ],
  129. ),
  130. );
  131. }
  132. bool _isFlashOn(String current) {
  133. return flashOn == current;
  134. }
  135. bool _isBackCamera(String current) {
  136. return backCamera == current;
  137. }
  138. Widget _buildQrView(BuildContext context) {
  139. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  140. double scanArea = (MediaQuery.of(context).size.width < 500 ||
  141. MediaQuery.of(context).size.height < 400)
  142. ? 150
  143. : 300;
  144. // To ensure the Scanner view is properly sizes after rotation
  145. // we need to listen for Flutter SizeChanged notification and update controller
  146. return NotificationListener<SizeChangedLayoutNotification>(
  147. onNotification: (notification) {
  148. Future.microtask(
  149. () => controller?.updateDimensions(qrKey, scanArea: scanArea));
  150. return false;
  151. },
  152. child: SizeChangedLayoutNotifier(
  153. key: const Key('qr-size-notifier'),
  154. child: QRView(
  155. key: qrKey,
  156. onQRViewCreated: _onQRViewCreated,
  157. overlay: QrScannerOverlayShape(
  158. borderColor: Colors.red,
  159. borderRadius: 10,
  160. borderLength: 30,
  161. borderWidth: 10,
  162. cutOutSize: scanArea,
  163. ),
  164. )));
  165. }
  166. void _onQRViewCreated(QRViewController controller) {
  167. this.controller = controller;
  168. controller.scannedDataStream.listen((scanData) {
  169. setState(() {
  170. result = scanData;
  171. });
  172. });
  173. }
  174. @override
  175. void dispose() {
  176. controller.dispose();
  177. super.dispose();
  178. }
  179. }