The QR Code Scanner package for Appeto SDK.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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