|
- import 'dart:io' show Platform;
-
- import 'package:barcode_scan/barcode_scan.dart';
- import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart';
- import 'package:farm_tpf/presentation/screens/home/home.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:fluttertoast/fluttertoast.dart';
-
- class ScanBarcodeScreen extends StatefulWidget {
- @override
- _ScanBarcodeScreenState createState() => _ScanBarcodeScreenState();
- }
-
- class _ScanBarcodeScreenState extends State<ScanBarcodeScreen> {
- ScanResult scanResult;
-
- var _aspectTolerance = 0.00;
- var _selectedCamera = -1;
- var _useAutoFocus = true;
- var _autoEnableFlash = false;
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- FlutterToast flutterToast;
-
- @override
- // ignore: type_annotate_public_apis
- initState() {
- super.initState();
- flutterToast = FlutterToast(context);
- scan();
- }
-
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(
- title: Text("Quét mã QR"),
- ),
- body: Center(
- child: scanResult == null ? Container() : Text(scanResult?.rawContent),
- ),
- );
- }
-
- Future scan() async {
- try {
- var options = ScanOptions(
- strings: {
- "cancel": "Huỷ",
- "flash_on": "Bật flash",
- "flash_off": "Tắt flash",
- },
- useCamera: _selectedCamera,
- autoEnableFlash: _autoEnableFlash,
- android: AndroidOptions(
- aspectTolerance: _aspectTolerance,
- useAutoFocus: _useAutoFocus,
- ),
- );
- var result = await BarcodeScanner.scan(options: options);
- setState(() {
- scanResult = result;
- if (1 != 1) {
- Navigator.of(context).pop();
- Navigator.of(context)
- .push(MaterialPageRoute(builder: (_) => HomePage()));
- } else {
- flutterToast.showToast(
- child:
- WidgetToast(message: "Mã QR không đúng", color: Colors.red));
- Navigator.of(context).pop();
- }
- });
- } on PlatformException catch (e) {
- var result = ScanResult(
- type: ResultType.Error,
- format: BarcodeFormat.unknown,
- );
-
- if (e.code == BarcodeScanner.cameraAccessDenied) {
- setState(() {
- result.rawContent = 'Vui lòng cấp quyền truy cập camera';
- });
- } else {
- result.rawContent = 'Vui lòng cấp quyền truy cập camera.';
- }
- setState(() {
- scanResult = result;
- });
- flutterToast.showToast(
- child: WidgetToast(
- message: "Vui lòng cấp quyền truy cập camera.",
- color: Colors.red));
- Navigator.of(context).pop();
- }
- }
- }
|