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.
 
 
 
 
 
 

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