Flutter - Show Camera/BarcodeScanner in Container

◇◆丶佛笑我妖孽 提交于 2021-02-10 12:11:49

问题


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://imgur.com/a/0qFvHto

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";
      });
    }
  }


回答1:


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

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,
            )
          ],
        ),
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/57177135/flutter-show-camera-barcodescanner-in-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!