You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.4 KiB
Dart
119 lines
3.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:qr_code_scanner/qr_code_scanner.dart';
|
|
import 'package:qr_code_tools/qr_code_tools.dart';
|
|
import 'package:test_sa/extensions/context_extension.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
import 'package:test_sa/extensions/text_extensions.dart';
|
|
|
|
import '../../../new_views/common_widgets/app_filled_button.dart';
|
|
import '../../../new_views/common_widgets/custom_app_bar.dart';
|
|
|
|
class ScanQrWidget extends StatefulWidget {
|
|
const ScanQrWidget({Key key, this.title}) : super(key: key);
|
|
final String title;
|
|
|
|
@override
|
|
_ScanQrWidgetState createState() => _ScanQrWidgetState();
|
|
}
|
|
|
|
class _ScanQrWidgetState extends State<ScanQrWidget> {
|
|
Barcode result;
|
|
QRViewController _controller;
|
|
bool _scanDone = false;
|
|
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR_scanner');
|
|
|
|
// 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() {
|
|
super.reassemble();
|
|
if (Platform.isAndroid) {
|
|
_controller?.pauseCamera();
|
|
} else if (Platform.isIOS) {
|
|
_controller?.resumeCamera();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
_controller?.dispose();
|
|
}
|
|
|
|
|
|
_pickManually() async {
|
|
final picker = ImagePicker();
|
|
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
|
|
|
|
if (pickedFile != null) {
|
|
try {
|
|
String qrData = await QrCodeToolsPlugin.decodeFrom(pickedFile.path);
|
|
if (qrData != null && !_scanDone) {
|
|
setState(() {
|
|
_scanDone = true;
|
|
});
|
|
Navigator.of(context).pop(qrData);
|
|
}
|
|
} catch (e) {
|
|
print('Error QR code: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Stack(
|
|
children: [
|
|
QRView(
|
|
key: qrKey,
|
|
onQRViewCreated: (QRViewController controller) {
|
|
setState(() {
|
|
_controller = controller;
|
|
});
|
|
controller.scannedDataStream.listen((scanData) {
|
|
if (!_scanDone) {
|
|
_scanDone = true;
|
|
Navigator.of(context).pop(scanData.code);
|
|
}
|
|
});
|
|
},
|
|
),
|
|
Center(
|
|
child: 'scan'.toSvgAsset(
|
|
height: 283.toScreenHeight.toInt(),
|
|
width: 283.toScreenWidth.toInt(),
|
|
fit: BoxFit.fitHeight,
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: 60.toScreenHeight,
|
|
child: CustomAppBar(
|
|
title: widget.title ?? '',
|
|
bottomCorner: 12,
|
|
)),
|
|
],
|
|
),
|
|
),
|
|
bottomSheet: Container(
|
|
height: 82.toScreenHeight,
|
|
color: Colors.white,
|
|
width: MediaQuery.of(context).size.width,
|
|
child: Center(
|
|
child: SizedBox(
|
|
height: 50.toScreenHeight,
|
|
width: 358.toScreenWidth,
|
|
child: AppFilledButton(
|
|
label: context.translation.pickManually,
|
|
onPressed: _pickManually,
|
|
),
|
|
),
|
|
),
|
|
));
|
|
}
|
|
}
|