The QR Code Scanner package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

144 rivejä
4.4 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. ),
  31. flex: 4,
  32. ),
  33. Expanded(
  34. child: Column(
  35. children: <Widget>[
  36. Text("This is the result of scan: $qrText"),
  37. Row(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. crossAxisAlignment: CrossAxisAlignment.center,
  40. children: <Widget>[
  41. Container(
  42. margin: EdgeInsets.all(8.0),
  43. child: RaisedButton(
  44. onPressed: () {
  45. if (controller != null) {
  46. controller.toggleFlash();
  47. if (_isFlashOn(flashState))
  48. setState(() {
  49. flashState = flash_off;
  50. });
  51. else
  52. setState(() {
  53. flashState = flash_on;
  54. });
  55. }
  56. },
  57. child: Text(flashState, style: TextStyle(fontSize: 20)),
  58. ),
  59. ),
  60. Container(
  61. margin: EdgeInsets.all(8.0),
  62. child: RaisedButton(
  63. onPressed: () {
  64. if (controller != null) {
  65. controller.flipCamera();
  66. if (_isBackCamera(cameraState))
  67. setState(() {
  68. cameraState = front_camera;
  69. });
  70. else
  71. setState(() {
  72. cameraState = back_camera;
  73. });
  74. }
  75. },
  76. child:
  77. Text(cameraState, style: TextStyle(fontSize: 20)),
  78. ),
  79. )
  80. ],
  81. ),
  82. Row(
  83. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  84. crossAxisAlignment: CrossAxisAlignment.center,
  85. children: <Widget>[
  86. Container(
  87. margin: EdgeInsets.only(bottom: 8.0),
  88. child: RaisedButton(
  89. onPressed: () {
  90. controller?.pauseCamera();
  91. },
  92. child: Text('pause', style: TextStyle(fontSize: 20)),
  93. ),
  94. ),
  95. Container(
  96. margin: EdgeInsets.only(bottom: 8.0),
  97. child: RaisedButton(
  98. onPressed: () {
  99. controller.resumeCamera();
  100. },
  101. child: Text('resume', style: TextStyle(fontSize: 20)),
  102. ),
  103. )
  104. ],
  105. ),
  106. ],
  107. ),
  108. flex: 1,
  109. )
  110. ],
  111. ),
  112. );
  113. }
  114. _isFlashOn(String current) {
  115. return flash_on == current;
  116. }
  117. _isBackCamera(String current) {
  118. return back_camera == current;
  119. }
  120. void _onQRViewCreated(QRViewController controller) {
  121. this.controller = controller;
  122. controller.scannedData.listen((scanData) {
  123. setState(() {
  124. qrText = scanData;
  125. });
  126. });
  127. }
  128. @override
  129. void dispose() {
  130. controller.dispose();
  131. super.dispose();
  132. }
  133. }