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.
 
 
 
 
 
 

189 lignes
6.2 KiB

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