import 'dart:io';

import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';


class ScanQrView extends StatefulWidget {
  const ScanQrView({Key? key}) : super(key: key);

  @override
  _ScanQrViewState createState() => _ScanQrViewState();
}

class _ScanQrViewState extends State<ScanQrView> {
  // 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();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: 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);
                }
              });
            },
            overlay: QrScannerOverlayShape(borderColor: Colors.red, borderRadius: 10, borderLength: 30, borderWidth: 10, cutOutSize: 280),
          ),
          Padding(
            padding: const EdgeInsets.all(12.0),
            child:  SizedBox(
              height: 100.toScreenHeight,
                child: DefaultAppBar(title: context.translation.scanQr)),
          )
        ],
      ),
    );
  }
}
