Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:developer';
- import 'dart:io';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:qr_code_scanner/qr_code_scanner.dart';
- import 'model/undangan.dart';
- import 'package:qrcode2/apiTamu.dart';
- 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');
- @override
- void reassemble() {
- super.reassemble();
- if (Platform.isAndroid) {
- controller?.pauseCamera();
- }
- controller?.resumeCamera();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Column(
- children: <Widget>[
- if (result == null)
- Expanded(flex: 4, child: _buildQrView(context))
- else
- FutureBuilder<Undangan?>(
- future: ApiUndangan.cekUndangan(result!.code!),
- builder:
- (BuildContext context, AsyncSnapshot<Undangan?> snapshot) {
- if (snapshot.hasData) {
- return _profil(snapshot.data!);
- } else if (snapshot.hasError) {
- print('Error snapshot');
- return Container(
- alignment: Alignment.center,
- margin: const EdgeInsets.only(top: 100),
- child: const Text(
- 'data tidak di temukan',
- style: TextStyle(fontSize: 28, color: Colors.red),
- ),
- );
- }
- return const CircularProgressIndicator();
- },
- ),
- Expanded(
- flex: 1,
- child: FittedBox(
- fit: BoxFit.contain,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- if (result != null)
- Text(
- 'Barcode Type: ${describeEnum(result!.format)} Data: ${result!.code}')
- else
- const Text('Scan a code'),
- ElevatedButton(
- onPressed: () {
- setState(() {
- result = null;
- });
- },
- child: const Text('coba lagi'),
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Container(
- margin: const EdgeInsets.all(8),
- child: ElevatedButton(
- onPressed: () async {
- await controller?.toggleFlash();
- setState(() {});
- },
- child: FutureBuilder(
- future: controller?.getFlashStatus(),
- builder: (context, snapshot) {
- return Text('Flash: ${snapshot.data}');
- },
- ),
- ),
- ),
- Container(
- margin: const EdgeInsets.all(8),
- child: ElevatedButton(
- onPressed: () async {
- await controller?.flipCamera();
- setState(() {});
- },
- child: FutureBuilder(
- future: controller?.getCameraInfo(),
- builder: (context, snapshot) {
- if (snapshot.data != null) {
- return Text(
- 'Camera facing ${describeEnum(snapshot.data!)}');
- } else {
- return const Text('loading');
- }
- },
- ),
- ),
- ),
- ],
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Container(
- margin: const EdgeInsets.all(8),
- child: ElevatedButton(
- onPressed: () async {
- await controller?.pauseCamera();
- },
- child: const Text(
- 'pause',
- style: TextStyle(fontSize: 20),
- ),
- ),
- ),
- Container(
- margin: const EdgeInsets.all(8),
- child: ElevatedButton(
- onPressed: () async {
- await controller?.resumeCamera();
- },
- child: const Text('resume',
- style: TextStyle(fontSize: 20)),
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- )
- ],
- ),
- );
- }
- 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 = (MediaQuery.of(context).size.width < 400 ||
- MediaQuery.of(context).size.height < 400)
- ? 400.0
- : 400.0;
- // To ensure the Scanner view is properly sizes after rotation
- // we need to listen for Flutter SizeChanged notification and update controller
- return QRView(
- key: qrKey,
- onQRViewCreated: _onQRViewCreated,
- overlay: QrScannerOverlayShape(
- borderColor: Colors.red,
- borderRadius: 10,
- borderLength: 30,
- borderWidth: 10,
- cutOutSize: scanArea,
- ),
- onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
- );
- }
- Widget _profil(Undangan undangan) {
- return Container(
- padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
- margin: const EdgeInsets.fromLTRB(10, 50, 10, 50),
- color: Colors.blue.withOpacity(9),
- child: Column(
- children: [
- Text(
- undangan.id,
- style: const TextStyle(
- fontWeight: FontWeight.bold, fontSize: 22, color: Colors.white),
- ),
- const SizedBox(height: 20),
- Text('Terimakasih sudah hadir di ${undangan.title}'),
- ],
- ),
- );
- }
- void _onQRViewCreated(QRViewController controller) {
- setState(() {
- this.controller = controller;
- });
- controller.scannedDataStream.listen((scanData) {
- setState(() {
- result = scanData;
- });
- });
- }
- void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
- log('${DateTime.now().toIso8601String()}_onPermissionSet $p');
- if (!p) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('no Permission')),
- );
- }
- }
- // @override
- // void dispose() {
- // controller?.dispose();
- // super.dispose();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement