The QR Code Scanner package for Appeto SDK.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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