The QR Code Scanner package for Appeto SDK.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

182 lines
5.8 KiB

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:qr_code_scanner/qr_code_scanner.dart';
  4. void main() => runApp(MaterialApp(home: QRViewExample()));
  5. const flashOn = 'FLASH ON';
  6. const flashOff = 'FLASH OFF';
  7. const frontCamera = 'FRONT CAMERA';
  8. const backCamera = '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. Barcode result;
  18. var flashState = flashOn;
  19. var cameraState = frontCamera;
  20. QRViewController controller;
  21. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  22. @override
  23. void reassemble() {
  24. super.reassemble();
  25. controller.pauseCamera();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. String resultText;
  30. if (result == null) {
  31. resultText = 'No previous result';
  32. } else {
  33. resultText =
  34. 'This is the result of scan (${describeEnum(result.format)}): ${result.code}';
  35. }
  36. return Scaffold(
  37. body: Column(
  38. children: <Widget>[
  39. Expanded(flex: 4, child: _buildQrView(context)),
  40. Expanded(
  41. flex: 1,
  42. child: FittedBox(
  43. fit: BoxFit.contain,
  44. child: Column(
  45. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  46. children: <Widget>[
  47. Text(resultText),
  48. Row(
  49. mainAxisAlignment: MainAxisAlignment.center,
  50. crossAxisAlignment: CrossAxisAlignment.center,
  51. children: <Widget>[
  52. Container(
  53. margin: EdgeInsets.all(8),
  54. child: RaisedButton(
  55. onPressed: () {
  56. if (controller != null) {
  57. controller.toggleFlash();
  58. if (_isFlashOn(flashState)) {
  59. setState(() {
  60. flashState = flashOff;
  61. });
  62. } else {
  63. setState(() {
  64. flashState = flashOn;
  65. });
  66. }
  67. }
  68. },
  69. child:
  70. Text(flashState, style: TextStyle(fontSize: 20)),
  71. ),
  72. ),
  73. Container(
  74. margin: EdgeInsets.all(8),
  75. child: RaisedButton(
  76. onPressed: () {
  77. if (controller != null) {
  78. controller.flipCamera();
  79. if (_isBackCamera(cameraState)) {
  80. setState(() {
  81. cameraState = frontCamera;
  82. });
  83. } else {
  84. setState(() {
  85. cameraState = backCamera;
  86. });
  87. }
  88. }
  89. },
  90. child:
  91. Text(cameraState, style: TextStyle(fontSize: 20)),
  92. ),
  93. )
  94. ],
  95. ),
  96. Row(
  97. mainAxisAlignment: MainAxisAlignment.center,
  98. crossAxisAlignment: CrossAxisAlignment.center,
  99. children: <Widget>[
  100. Container(
  101. margin: EdgeInsets.all(8),
  102. child: RaisedButton(
  103. onPressed: () {
  104. controller?.pauseCamera();
  105. },
  106. child: Text('pause', style: TextStyle(fontSize: 20)),
  107. ),
  108. ),
  109. Container(
  110. margin: EdgeInsets.all(8),
  111. child: RaisedButton(
  112. onPressed: () {
  113. controller?.resumeCamera();
  114. },
  115. child: Text('resume', style: TextStyle(fontSize: 20)),
  116. ),
  117. )
  118. ],
  119. ),
  120. ],
  121. ),
  122. ),
  123. )
  124. ],
  125. ),
  126. );
  127. }
  128. bool _isFlashOn(String current) {
  129. return flashOn == current;
  130. }
  131. bool _isBackCamera(String current) {
  132. return backCamera == current;
  133. }
  134. Widget _buildQrView(BuildContext context) {
  135. // To ensure the Scanner view is properly sizes after rotation
  136. // we need to listen for Flutter SizeChanged notification and update controller
  137. return NotificationListener<SizeChangedLayoutNotification>(
  138. onNotification: (notification) {
  139. Future.microtask(() => controller?.updateDimensions(qrKey));
  140. return false;
  141. },
  142. child: SizeChangedLayoutNotifier(
  143. key: const Key('qr-size-notifier'),
  144. child: QRView(
  145. key: qrKey,
  146. onQRViewCreated: _onQRViewCreated,
  147. overlay: QrScannerOverlayShape(
  148. borderColor: Colors.red,
  149. borderRadius: 10,
  150. borderLength: 30,
  151. borderWidth: 10,
  152. cutOutSize: 300,
  153. ),
  154. )));
  155. }
  156. void _onQRViewCreated(QRViewController controller) {
  157. this.controller = controller;
  158. controller.scannedDataStream.listen((scanData) {
  159. setState(() {
  160. result = scanData;
  161. });
  162. });
  163. }
  164. @override
  165. void dispose() {
  166. controller.dispose();
  167. super.dispose();
  168. }
  169. }