The QR Code Scanner package for Appeto SDK.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

121 líneas
3.6 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:qr_code_scanner/qr_code_scanner.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. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  18. var qrText = "";
  19. var flashState = flash_on;
  20. var cameraState = front_camera;
  21. QRViewController controller;
  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. ),
  32. flex: 4,
  33. ),
  34. Expanded(
  35. child: Column(
  36. children: <Widget>[
  37. Text("This is the result of scan: $qrText"),
  38. Row(
  39. mainAxisAlignment: MainAxisAlignment.center,
  40. crossAxisAlignment: CrossAxisAlignment.center,
  41. children: <Widget>[
  42. Container(
  43. margin: EdgeInsets.all(8.0),
  44. child: RaisedButton(
  45. onPressed: () {
  46. if (controller != null) {
  47. controller.flipFlash();
  48. if (_isFlashOn(flashState))
  49. setState(() {
  50. flashState = flash_off;
  51. });
  52. else
  53. setState(() {
  54. flashState = flash_on;
  55. });
  56. }
  57. },
  58. child: Text(flashState, style: TextStyle(fontSize: 20)),
  59. ),
  60. ),
  61. Container(
  62. margin: EdgeInsets.all(8.0),
  63. child: RaisedButton(
  64. onPressed: () {
  65. if (controller != null) {
  66. controller.flipCamera();
  67. if (_isBackCamera(cameraState))
  68. setState(() {
  69. cameraState = front_camera;
  70. });
  71. else
  72. setState(() {
  73. cameraState = back_camera;
  74. });
  75. }
  76. },
  77. child:
  78. Text(cameraState, style: TextStyle(fontSize: 20)),
  79. ),
  80. )
  81. ],
  82. )
  83. ],
  84. ),
  85. flex: 1,
  86. )
  87. ],
  88. ),
  89. );
  90. }
  91. _isFlashOn(String current) {
  92. return flash_on == current;
  93. }
  94. _isBackCamera(String current) {
  95. return back_camera == current;
  96. }
  97. void _onQRViewCreated(QRViewController controller) {
  98. final channel = controller.channel;
  99. controller.init(qrKey);
  100. this.controller = controller;
  101. channel.setMethodCallHandler((MethodCall call) async {
  102. switch (call.method) {
  103. case "onRecognizeQR":
  104. dynamic arguments = call.arguments;
  105. setState(() {
  106. qrText = arguments.toString();
  107. });
  108. }
  109. });
  110. }
  111. }