import 'dart:developer';
import 'dart:io';
import 'package:flutter/[Link]';
import 'package:flutter/[Link]';
import 'package:qr_code_scanner/qr_code_scanner.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({[Link]});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: [Link](seedColor: [Link]),
useMaterial3: true,
),
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget {
const MyHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Demo Home Page')),
body: Center(
child: ElevatedButton(
onPressed: () {
[Link](context).push(MaterialPageRoute(
builder: (context) => const QRViewExample(),
));
},
child: const Text('qrView'),
),
),
);
}
}
class QRViewExample extends StatefulWidget {
const QRViewExample({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _QRViewExampleState();
}
class _QRViewExampleState extends State<QRViewExample> {
Barcode? result;
QRViewController? controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
@override
void reassemble() {
[Link]();
if ([Link]) {
controller!.pauseCamera();
}
controller!.resumeCamera();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 100,
),
Expanded(flex: 1, child: _buildQrView(context)),
Expanded(
flex: 1,
child: FittedBox(
fit: [Link],
child: Column(
mainAxisAlignment: [Link],
children: <Widget>[
if (result != null)
Column(
children: [
Icon(
Icons.qr_code_2,
size: 20,
),
SizedBox(
width: 10,
),
Text(
'Barcode Type: ${describeEnum(result!.format)} '),
Text(' Data: ${result!.code}'),
],
)
else
const Text('Scan a code'),
Row(
mainAxisAlignment: [Link],
crossAxisAlignment: [Link],
children: <Widget>[
Container(
margin: const [Link](8),
child: ElevatedButton(
onPressed: () async {
await controller?.toggleFlash();
setState(() {});
},
child: FutureBuilder(
future: controller?.getFlashStatus(),
builder: (context, snapshot) {
IconData flashIcon = Icons
.flashlight_off_outlined; // Default to flash off
icon
if ([Link] ==
[Link]) {
// If future completed, check snapshot data
if ([Link] &&
[Link] != null &&
[Link]!) {
flashIcon = Icons
.flashlight_on_outlined; // If true, set
flash on icon
}
}
return Row(
children: [
Icon(
flashIcon,
size: 20,
),
SizedBox(
width: 10,
),
Text(
'Flash: ${[Link] ?? "Loading..."}',
style: TextStyle(fontSize: 12)),
],
);
},
),
),
),
Container(
margin: const [Link](8),
child: ElevatedButton(
onPressed: () async {
await controller?.flipCamera();
setState(() {});
},
child: FutureBuilder(
future: controller?.getCameraInfo(),
builder: (context, snapshot) {
if ([Link] != null) {
return Row(
children: [
Icon(
Icons.cameraswitch_outlined,
size: 20,
),
SizedBox(
width: 10,
),
// Text(
// 'Camera facing $
{describeEnum([Link]!)}'),
Text('${describeEnum([Link]!)}',
style: TextStyle(fontSize: 12)),
],
);
} else {
return const Text('loading');
}
},
)),
)
],
),
Row(
mainAxisAlignment: [Link],
crossAxisAlignment: [Link],
children: <Widget>[
Container(
margin: const [Link](8),
child: ElevatedButton(
onPressed: () async {
await controller?.pauseCamera();
},
child: Row(
children: [
Icon(
[Link],
size: 20,
),
SizedBox(
width: 10,
),
const Text('pause',
style: TextStyle(fontSize: 12)),
],
),
),
),
Container(
margin: const [Link](8),
child: ElevatedButton(
onPressed: () async {
await controller?.resumeCamera();
},
child: Row(
children: [
Row(
children: [
Icon(
Icons.play_arrow,
size: 20,
),
SizedBox(
width: 10,
),
const Text('resume',
style: TextStyle(fontSize: 12)),
],
),
],
),
),
)
],
),
],
),
),
)
],
),
);
}
Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the
scanArea and overlay accordingly.
var scanArea = ([Link](context).[Link] < 500 ||
[Link](context).[Link] < 500)
? 250.0
: 300.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return Padding(
padding: const [Link](16.0),
child: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: [Link],
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea,
),
onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
),
);
}
void _onQRViewCreated(QRViewController controller) {
setState(() {
[Link] = controller;
});
[Link]((scanData) {
setState(() {
result = scanData;
});
});
}
void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
log('${[Link]().toIso8601String()}_onPermissionSet $p');
if (!p) {
[Link](context).showSnackBar(
const SnackBar(content: Text('no Permission')),
);
}
}
@override
void dispose() {
controller?.dispose();
[Link]();
}
}