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.
 
 
 
 
 
 

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