The QR Code Scanner package for Appeto SDK.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

161 satır
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. }
  25. controller.resumeCamera();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. body: Column(
  31. children: <Widget>[
  32. Expanded(flex: 4, child: _buildQrView(context)),
  33. Expanded(
  34. flex: 1,
  35. child: FittedBox(
  36. fit: BoxFit.contain,
  37. child: Column(
  38. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  39. children: <Widget>[
  40. if (result != null)
  41. Text(
  42. 'Barcode Type: ${describeEnum(result.format)} Data: ${result.code}')
  43. else
  44. Text('Scan a code'),
  45. Row(
  46. mainAxisAlignment: MainAxisAlignment.center,
  47. crossAxisAlignment: CrossAxisAlignment.center,
  48. children: <Widget>[
  49. Container(
  50. margin: EdgeInsets.all(8),
  51. child: ElevatedButton(
  52. onPressed: () async {
  53. await controller?.toggleFlash();
  54. setState(() {});
  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: ElevatedButton(
  66. onPressed: () async {
  67. await controller?.flipCamera();
  68. setState(() {});
  69. },
  70. child: FutureBuilder(
  71. future: controller?.getCameraInfo(),
  72. builder: (context, snapshot) {
  73. if (snapshot.data != null) {
  74. return Text(
  75. 'Camera facing ${describeEnum(snapshot.data)}');
  76. } else {
  77. return Text('loading');
  78. }
  79. },
  80. )),
  81. )
  82. ],
  83. ),
  84. Row(
  85. mainAxisAlignment: MainAxisAlignment.center,
  86. crossAxisAlignment: CrossAxisAlignment.center,
  87. children: <Widget>[
  88. Container(
  89. margin: EdgeInsets.all(8),
  90. child: ElevatedButton(
  91. onPressed: () async {
  92. await controller?.pauseCamera();
  93. },
  94. child: Text('pause', style: TextStyle(fontSize: 20)),
  95. ),
  96. ),
  97. Container(
  98. margin: EdgeInsets.all(8),
  99. child: ElevatedButton(
  100. onPressed: () async {
  101. await controller?.resumeCamera();
  102. },
  103. child: Text('resume', style: TextStyle(fontSize: 20)),
  104. ),
  105. )
  106. ],
  107. ),
  108. ],
  109. ),
  110. ),
  111. )
  112. ],
  113. ),
  114. );
  115. }
  116. Widget _buildQrView(BuildContext context) {
  117. // For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
  118. var scanArea = (MediaQuery.of(context).size.width < 400 ||
  119. MediaQuery.of(context).size.height < 400)
  120. ? 150.0
  121. : 300.0;
  122. // To ensure the Scanner view is properly sizes after rotation
  123. // we need to listen for Flutter SizeChanged notification and update controller
  124. return QRView(
  125. key: qrKey,
  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. void _onQRViewCreated(QRViewController controller) {
  136. setState(() {
  137. this.controller = controller;
  138. });
  139. controller.scannedDataStream.listen((scanData) {
  140. setState(() {
  141. result = scanData;
  142. });
  143. });
  144. }
  145. @override
  146. void dispose() {
  147. controller?.dispose();
  148. super.dispose();
  149. }
  150. }