The QR Code Scanner package for Appeto SDK.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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