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.
 
 
 
 
 
 

165 lignes
5.5 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. cameraFacing: CameraFacing.front,
  128. onQRViewCreated: _onQRViewCreated,
  129. formatsAllowed: [BarcodeFormat.qrcode],
  130. overlay: QrScannerOverlayShape(
  131. borderColor: Colors.red,
  132. borderRadius: 10,
  133. borderLength: 30,
  134. borderWidth: 10,
  135. cutOutSize: scanArea,
  136. ),
  137. );
  138. }
  139. void _onQRViewCreated(QRViewController controller) {
  140. setState(() {
  141. this.controller = controller;
  142. });
  143. controller.scannedDataStream.listen((scanData) {
  144. setState(() {
  145. result = scanData;
  146. });
  147. });
  148. }
  149. @override
  150. void dispose() {
  151. controller?.dispose();
  152. super.dispose();
  153. }
  154. }