CodOG
CodOG

Reputation: 99

Flutter - Show Camera/BarcodeScanner in Container

I would like to store the camera function in a separate container as the title already says. that means, if the camera starts it is only displayed in the red container, how could i achieve this at this point?

Picture of my Screen: https://i.sstatic.net/R5INL.jpg

I use the following dependency for Scanning:

barcode_scan: ^1.0.0 


My Code for scanning looks like this: 

Future _scanQR() async {
    try {
      String qrResult = await BarcodeScanner.scan();
      setState(() {
        result = qrResult;
        Future.delayed(const Duration(seconds: 1));
        showAlertDialog(context);
      });
    } on PlatformException catch (ex) {
      if (ex.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          result = "Zugriff auf die Kamera wurde nicht gewährt!";
        });
      } else {
        setState(() {
          result = "Unknown Error $ex";
        });
      }
    } on FormatException {
      setState(() {
        result = "Scan fehlgeschlagen";
      });
    } catch (ex) {
      setState(() {
        result = "Unknown Error $ex";
      });
    }
  }

Upvotes: 3

Views: 2826

Answers (1)

chunhunghan
chunhunghan

Reputation: 54427

please check method used by this package https://github.com/elratonmaton/LastQrScanner
or use this package instead

Dart code of this package use AndroidView and UiKitView
code snippet

class _QRViewState extends State<LastQrScannerPreview> {
  @override
  Widget build(BuildContext context) {
    var androidView = AndroidView(
      viewType: 'last_qr_scanner/qrview',
      onPlatformViewCreated: _onPlatformViewCreated,
    );

    if (defaultTargetPlatform == TargetPlatform.android) {
      return androidView;
    }

    if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: 'last_qr_scanner/qrview',
        onPlatformViewCreated: _onPlatformViewCreated,
        creationParams: _CreationParams.fromWidget(0, 0).toMap(),
        creationParamsCodec: StandardMessageCodec(),
      );
    }

and more detail on Kotlin part, you can referce source code
Result in emulator

enter image description here

you can see body of Widget build use LastQrScannerPreview in example code

body: Column(
          children: <Widget>[
            Expanded(
              child: LastQrScannerPreview(
                key: qrKey,
                onQRViewCreated: _onQRViewCreated,
              ),
              flex: 4,
            ),

full example code of this package

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:last_qr_scanner/last_qr_scanner.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  const MyApp({
    Key key,
  }) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  var qrText = ""; 
  var controller;

  @override
  void initState() {
    super.initState();
  }

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    final channel = controller.channel;
    controller.init(qrKey);
    channel.setMethodCallHandler((MethodCall call) async {
      switch (call.method) {
        case "onRecognizeQR":
          dynamic arguments = call.arguments;
          setState(() {
            qrText = arguments.toString();
          });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Barcode Scanner Example'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: LastQrScannerPreview(
                key: qrKey,
                onQRViewCreated: _onQRViewCreated,
              ),
              flex: 4,
            ),
            Expanded(
              child: Text("This is the result of scan: $qrText"),
              flex: 1,
            ),
            Expanded(
              child: RaisedButton(
                onPressed: () {
                  this.controller.toggleTorch();                  
                },
                child: Text("Toggle Torch"),
              ),
              flex: 1,
            )
          ],
        ),
      ),
    );
  }
}

Upvotes: 1

Related Questions