The QR Code Scanner package for Appeto SDK.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

162 lignes
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: RaisedButton(
  53. onPressed: () => setState(() {
  54. controller?.toggleFlash();
  55. }),
  56. child: FutureBuilder(
  57. future: controller?.getFlashStatus(),
  58. builder: (context, snapshot) {
  59. return Text('Flash: ${snapshot.data}');
  60. },
  61. )),
  62. ),
  63. Container(
  64. margin: EdgeInsets.all(8),
  65. child: RaisedButton(
  66. onPressed: () => setState(() {
  67. controller?.flipCamera();
  68. }),
  69. child: FutureBuilder(
  70. future: controller?.getCameraInfo(),
  71. builder: (context, snapshot) {
  72. if (snapshot.data != null) {
  73. return Text(
  74. 'Camera facing ${describeEnum(snapshot.data)}');
  75. } else {
  76. return Text('loading');
  77. }
  78. },
  79. )),
  80. )
  81. ],
  82. ),
  83. Row(
  84. mainAxisAlignment: MainAxisAlignment.center,
  85. crossAxisAlignment: CrossAxisAlignment.center,
  86. children: <Widget>[
  87. Container(
  88. margin: EdgeInsets.all(8),
  89. child: RaisedButton(
  90. onPressed: () {
  91. controller?.pauseCamera();
  92. },
  93. child: Text('pause', style: TextStyle(fontSize: 20)),
  94. ),
  95. ),
  96. Container(
  97. margin: EdgeInsets.all(8),
  98. child: RaisedButton(
  99. onPressed: () {
  100. controller?.resumeCamera();
  101. },
  102. child: Text('resume', style: TextStyle(fontSize: 20)),
  103. ),
  104. )
  105. ],
  106. ),
  107. ],
  108. ),
  109. ),
  110. )
  111. ],
  112. ),
  113. );
  114. }
  115. Widget _buildQrView(BuildContext context) {
  116. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  117. var scanArea = (MediaQuery.of(context).size.width < 400 ||
  118. MediaQuery.of(context).size.height < 400)
  119. ? 150.0
  120. : 300.0;
  121. // To ensure the Scanner view is properly sizes after rotation
  122. // we need to listen for Flutter SizeChanged notification and update controller
  123. return QRView(
  124. key: qrKey,
  125. cameraFacing: CameraFacing.front,
  126. onQRViewCreated: _onQRViewCreated,
  127. overlay: QrScannerOverlayShape(
  128. borderColor: Colors.red,
  129. borderRadius: 10,
  130. borderLength: 30,
  131. borderWidth: 10,
  132. cutOutSize: scanArea,
  133. ),
  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. }