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.
 
 
 
 
 
 

158 wiersze
5.0 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:qr_code_scanner/qr_code_scanner.dart';
  3. void main() => runApp(MaterialApp(home: QRViewExample()));
  4. const flash_on = "FLASH ON";
  5. const flash_off = "FLASH OFF";
  6. const front_camera = "FRONT CAMERA";
  7. const back_camera = "BACK CAMERA";
  8. class QRViewExample extends StatefulWidget {
  9. const QRViewExample({
  10. Key key,
  11. }) : super(key: key);
  12. @override
  13. State<StatefulWidget> createState() => _QRViewExampleState();
  14. }
  15. class _QRViewExampleState extends State<QRViewExample> {
  16. var qrText = "";
  17. var flashState = flash_on;
  18. var cameraState = front_camera;
  19. QRViewController controller;
  20. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  21. @override
  22. Widget build(BuildContext context) {
  23. return Scaffold(
  24. body: Column(
  25. children: <Widget>[
  26. Expanded(
  27. child: QRView(
  28. key: qrKey,
  29. onQRViewCreated: _onQRViewCreated,
  30. overlay: QrScannerOverlayShape(
  31. borderColor: Colors.red,
  32. borderRadius: 10,
  33. borderLength: 30,
  34. borderWidth: 10,
  35. cutOutSize: 300,
  36. ),
  37. ),
  38. flex: 4,
  39. ),
  40. Expanded(
  41. child: FittedBox(
  42. fit: BoxFit.contain,
  43. child: Column(
  44. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  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. },
  68. child:
  69. Text(flashState, style: TextStyle(fontSize: 20)),
  70. ),
  71. ),
  72. Container(
  73. margin: EdgeInsets.all(8.0),
  74. child: RaisedButton(
  75. onPressed: () {
  76. if (controller != null) {
  77. controller.flipCamera();
  78. if (_isBackCamera(cameraState)) {
  79. setState(() {
  80. cameraState = front_camera;
  81. });
  82. } else {
  83. setState(() {
  84. cameraState = back_camera;
  85. });
  86. }
  87. }
  88. },
  89. child:
  90. Text(cameraState, style: TextStyle(fontSize: 20)),
  91. ),
  92. )
  93. ],
  94. ),
  95. Row(
  96. mainAxisAlignment: MainAxisAlignment.center,
  97. crossAxisAlignment: CrossAxisAlignment.center,
  98. children: <Widget>[
  99. Container(
  100. margin: EdgeInsets.all(8.0),
  101. child: RaisedButton(
  102. onPressed: () {
  103. controller?.pauseCamera();
  104. },
  105. child: Text('pause', style: TextStyle(fontSize: 20)),
  106. ),
  107. ),
  108. Container(
  109. margin: EdgeInsets.all(8.0),
  110. child: RaisedButton(
  111. onPressed: () {
  112. controller?.resumeCamera();
  113. },
  114. child: Text('resume', style: TextStyle(fontSize: 20)),
  115. ),
  116. )
  117. ],
  118. ),
  119. ],
  120. ),
  121. ),
  122. flex: 1,
  123. )
  124. ],
  125. ),
  126. );
  127. }
  128. _isFlashOn(String current) {
  129. return flash_on == current;
  130. }
  131. _isBackCamera(String current) {
  132. return back_camera == current;
  133. }
  134. void _onQRViewCreated(QRViewController controller) {
  135. this.controller = controller;
  136. controller.scannedDataStream.listen((scanData) {
  137. setState(() {
  138. qrText = scanData;
  139. });
  140. });
  141. }
  142. @override
  143. void dispose() {
  144. controller.dispose();
  145. super.dispose();
  146. }
  147. }