280 lines
9.6 KiB
Dart
280 lines
9.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Article/article.dart';
|
|
import 'package:mymuseum_visitapp/constants.dart';
|
|
import 'package:qr_code_scanner/qr_code_scanner.dart';
|
|
|
|
|
|
class ScannerTEST extends StatefulWidget {
|
|
const ScannerTEST({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ScannerTEST> createState() => _ScannerTESTState();
|
|
}
|
|
|
|
class _ScannerTESTState extends State<ScannerTEST> {
|
|
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() {
|
|
print("reassemble ");
|
|
super.reassemble();
|
|
if (Platform.isAndroid) {
|
|
controller!.pauseCamera();
|
|
}
|
|
controller!.resumeCamera();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Size size = MediaQuery.of(context).size;
|
|
return Container(
|
|
height: size.height *0.5,
|
|
width: size.width *0.9,
|
|
child: Stack(
|
|
children: [
|
|
Center(
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
child: _buildQrView(context),
|
|
)
|
|
),
|
|
Positioned(
|
|
top: 0,
|
|
right: 0,
|
|
child: Container(
|
|
width: 45,
|
|
height: 45,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
color: kMainColor,
|
|
borderRadius: BorderRadius.circular(20.0),
|
|
),
|
|
margin: const EdgeInsets.all(8),
|
|
child: InkWell(
|
|
onTap: () async {
|
|
await controller?.toggleFlash();
|
|
setState(() {});
|
|
},
|
|
child: FutureBuilder(
|
|
future: controller?.getFlashStatus(),
|
|
builder: (context, snapshot) {
|
|
return const Icon(Icons.flash_on, color: Colors.white);
|
|
},
|
|
)),
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 0,
|
|
right: 0,
|
|
child: Container(
|
|
width: 45,
|
|
height: 45,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
color: kMainColor,
|
|
borderRadius: BorderRadius.circular(20.0),
|
|
),
|
|
margin: const EdgeInsets.all(8),
|
|
child: InkWell(
|
|
onTap: () async {
|
|
await controller?.flipCamera();
|
|
setState(() {});
|
|
},
|
|
child: FutureBuilder(
|
|
future: controller?.getCameraInfo(),
|
|
builder: (context, snapshot) {
|
|
return const Icon(Icons.flip_camera_android, color: Colors.white);
|
|
},
|
|
)),
|
|
/*child: FutureBuilder(
|
|
future: controller?.getFlashStatus(),
|
|
builder: (context, snapshot) {
|
|
return Icon(Icons.flip_camera_android, color: Colors.white);
|
|
},
|
|
)*/),
|
|
),
|
|
/*Column(
|
|
children: <Widget>[
|
|
Expanded(flex: 4, child: _buildQrView(context)),
|
|
Expanded(
|
|
flex: 1,
|
|
child: FittedBox(
|
|
fit: BoxFit.contain,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
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 Icon(Icons.flash_on);
|
|
},
|
|
)),
|
|
),
|
|
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 {
|
|
print(controller);
|
|
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)
|
|
? 225.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 QRView(
|
|
key: qrKey,
|
|
onQRViewCreated: _onQRViewCreated,
|
|
overlay: QrScannerOverlayShape(
|
|
borderColor: kMainColor,
|
|
borderRadius: 10,
|
|
borderLength: 25,
|
|
borderWidth: 5,
|
|
cutOutSize: 225.0),
|
|
onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
|
|
);
|
|
}
|
|
|
|
void _onQRViewCreated(QRViewController controller) {
|
|
setState(() {
|
|
this.controller = controller;
|
|
});
|
|
if (Platform.isAndroid) {
|
|
controller!.pauseCamera();
|
|
}
|
|
controller!.resumeCamera();
|
|
controller.scannedDataStream.listen((scanData) {
|
|
setState(() {
|
|
result = scanData;
|
|
print(result);
|
|
print(result!.code.toString());
|
|
var code = result == null ? "" : result!.code.toString();
|
|
if(result!.format == BarcodeFormat.qrcode) {
|
|
controller.pauseCamera();
|
|
print("QR CODE FOUND");
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('QR CODE FOUND - ${code.toString()}')),
|
|
);
|
|
|
|
// TODO search in local if data == Id of Article
|
|
//Navigator.of(context).pop();
|
|
|
|
// If so, navigate to article view
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return ArticlePage(articleId: code);
|
|
},
|
|
),
|
|
);
|
|
|
|
//Navigator.of(context).pop();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
showScannerDialog (String question, BuildContext context) {
|
|
showDialog(
|
|
builder: (BuildContext context) => const AlertDialog(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.all(Radius.circular(10.0))
|
|
),
|
|
content: ScannerTEST(
|
|
),
|
|
contentPadding: EdgeInsets.zero,
|
|
), context: context
|
|
);
|
|
}
|
|
|
|
|