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.
 
 
 
 
 
 

162 lines
5.4 KiB

  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:qr_code_scanner/qr_code_scanner.dart';
  5. void main() => runApp(MaterialApp(home: QRViewExample()));
  6. class QRViewExample extends StatefulWidget {
  7. const QRViewExample({
  8. Key key,
  9. }) : super(key: key);
  10. @override
  11. State<StatefulWidget> createState() => _QRViewExampleState();
  12. }
  13. class _QRViewExampleState extends State<QRViewExample> {
  14. Barcode result;
  15. QRViewController controller;
  16. final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  17. // In order to get hot reload to work we need to pause the camera if the platform
  18. // is android, or resume the camera if the platform is iOS.
  19. @override
  20. void reassemble() {
  21. super.reassemble();
  22. if (Platform.isAndroid) {
  23. controller.pauseCamera();
  24. } else if (Platform.isIOS) {
  25. controller.resumeCamera();
  26. }
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. body: Column(
  32. children: <Widget>[
  33. Expanded(flex: 4, child: _buildQrView(context)),
  34. Expanded(
  35. flex: 1,
  36. child: FittedBox(
  37. fit: BoxFit.contain,
  38. child: Column(
  39. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  40. children: <Widget>[
  41. if (result != null)
  42. Text(
  43. 'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
  44. else
  45. Text('Scan a code'),
  46. Row(
  47. mainAxisAlignment: MainAxisAlignment.center,
  48. crossAxisAlignment: CrossAxisAlignment.center,
  49. children: <Widget>[
  50. Container(
  51. margin: EdgeInsets.all(8),
  52. child: ElevatedButton(
  53. onPressed: () async {
  54. await controller?.toggleFlash();
  55. setState(() {});
  56. },
  57. child: FutureBuilder(
  58. future: controller?.getFlashStatus(),
  59. builder: (context, snapshot) {
  60. return Text('Flash: ${snapshot.data}');
  61. },
  62. )),
  63. ),
  64. Container(
  65. margin: EdgeInsets.all(8),
  66. child: ElevatedButton(
  67. onPressed: () async {
  68. await controller?.flipCamera();
  69. setState(() {});
  70. },
  71. child: FutureBuilder(
  72. future: controller?.getCameraInfo(),
  73. builder: (context, snapshot) {
  74. if (snapshot.data != null) {
  75. return Text(
  76. 'Camera facing ${describeEnum(snapshot.data)}');
  77. } else {
  78. return Text('loading');
  79. }
  80. },
  81. )),
  82. )
  83. ],
  84. ),
  85. Row(
  86. mainAxisAlignment: MainAxisAlignment.center,
  87. crossAxisAlignment: CrossAxisAlignment.center,
  88. children: <Widget>[
  89. Container(
  90. margin: EdgeInsets.all(8),
  91. child: ElevatedButton(
  92. onPressed: () async {
  93. await controller?.pauseCamera();
  94. },
  95. child: Text('pause', style: TextStyle(fontSize: 20)),
  96. ),
  97. ),
  98. Container(
  99. margin: EdgeInsets.all(8),
  100. child: ElevatedButton(
  101. onPressed: () async {
  102. await controller?.resumeCamera();
  103. },
  104. child: Text('resume', style: TextStyle(fontSize: 20)),
  105. ),
  106. )
  107. ],
  108. ),
  109. ],
  110. ),
  111. ),
  112. )
  113. ],
  114. ),
  115. );
  116. }
  117. Widget _buildQrView(BuildContext context) {
  118. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  119. var scanArea = (MediaQuery.of(context).size.width < 400 ||
  120. MediaQuery.of(context).size.height < 400)
  121. ? 150.0
  122. : 300.0;
  123. // To ensure the Scanner view is properly sizes after rotation
  124. // we need to listen for Flutter SizeChanged notification and update controller
  125. return QRView(
  126. key: qrKey,
  127. onQRViewCreated: _onQRViewCreated,
  128. overlay: QrScannerOverlayShape(
  129. borderColor: Colors.red,
  130. borderRadius: 10,
  131. borderLength: 30,
  132. borderWidth: 10,
  133. cutOutSize: scanArea),
  134. );
  135. }
  136. void _onQRViewCreated(QRViewController controller) {
  137. setState(() {
  138. this.controller = controller;
  139. });
  140. controller.scannedDataStream.listen((scanData) {
  141. setState(() {
  142. result = scanData;
  143. });
  144. });
  145. }
  146. @override
  147. void dispose() {
  148. controller?.dispose();
  149. super.dispose();
  150. }
  151. }