The QR Code Scanner package for Appeto SDK.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

156 wiersze
5.0 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:qr_code_scanner/qr_code_scanner.dart';
  3. import 'package:qr_code_scanner/qr_scanner_overlay_shape.dart';
  4. void main() => runApp(MaterialApp(home: QRViewExample()));
  5. const flash_on = "FLASH ON";
  6. const flash_off = "FLASH OFF";
  7. const front_camera = "FRONT CAMERA";
  8. const back_camera = "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. var qrText = "";
  18. var flashState = flash_on;
  19. var cameraState = front_camera;
  20. QRViewController controller;
  21. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. body: Column(
  26. children: <Widget>[
  27. Expanded(
  28. child: QRView(
  29. key: qrKey,
  30. onQRViewCreated: _onQRViewCreated,
  31. overlay: QrScannerOverlayShape(
  32. borderColor: Colors.red,
  33. borderRadius: 10,
  34. borderLength: 30,
  35. borderWidth: 10,
  36. cutOutSize: 300,
  37. ),
  38. ),
  39. flex: 4,
  40. ),
  41. Expanded(
  42. child: FittedBox(
  43. fit: BoxFit.contain,
  44. child: Column(
  45. children: <Widget>[
  46. Text("This is the result of scan: $qrText"),
  47. Row(
  48. mainAxisAlignment: MainAxisAlignment.center,
  49. crossAxisAlignment: CrossAxisAlignment.center,
  50. children: <Widget>[
  51. Container(
  52. margin: EdgeInsets.all(8.0),
  53. child: RaisedButton(
  54. onPressed: () {
  55. if (controller != null) {
  56. controller.toggleFlash();
  57. if (_isFlashOn(flashState))
  58. setState(() {
  59. flashState = flash_off;
  60. });
  61. else
  62. setState(() {
  63. flashState = flash_on;
  64. });
  65. }
  66. },
  67. child:
  68. Text(flashState, style: TextStyle(fontSize: 20)),
  69. ),
  70. ),
  71. Container(
  72. margin: EdgeInsets.all(8.0),
  73. child: RaisedButton(
  74. onPressed: () {
  75. if (controller != null) {
  76. controller.flipCamera();
  77. if (_isBackCamera(cameraState))
  78. setState(() {
  79. cameraState = front_camera;
  80. });
  81. else
  82. setState(() {
  83. cameraState = back_camera;
  84. });
  85. }
  86. },
  87. child:
  88. Text(cameraState, style: TextStyle(fontSize: 20)),
  89. ),
  90. )
  91. ],
  92. ),
  93. Row(
  94. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  95. crossAxisAlignment: CrossAxisAlignment.center,
  96. children: <Widget>[
  97. Container(
  98. margin: EdgeInsets.only(bottom: 8.0),
  99. child: RaisedButton(
  100. onPressed: () {
  101. controller?.pauseCamera();
  102. },
  103. child: Text('pause', style: TextStyle(fontSize: 20)),
  104. ),
  105. ),
  106. Container(
  107. margin: EdgeInsets.only(bottom: 8.0),
  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. flex: 1,
  121. )
  122. ],
  123. ),
  124. );
  125. }
  126. _isFlashOn(String current) {
  127. return flash_on == current;
  128. }
  129. _isBackCamera(String current) {
  130. return back_camera == current;
  131. }
  132. void _onQRViewCreated(QRViewController controller) {
  133. this.controller = controller;
  134. controller.scannedData.listen((scanData) {
  135. setState(() {
  136. qrText = scanData;
  137. });
  138. });
  139. }
  140. @override
  141. void dispose() {
  142. controller.dispose();
  143. super.dispose();
  144. }
  145. }