如何将Barcode_scan小部件作为其他小部件的子级使用?

时间:2019-10-30 06:55:46

标签: android flutter barcode barcode-scanner

我在flutter应用程序中使用barcode_scan小部件时,我调用了Scan方法,该小部件占据了显示相机的整个屏幕,我想在另一个小部件中显示camera view

>

2 个答案:

答案 0 :(得分:0)

您可以使用软件包https://pub.dev/packages/last_qr_scannerhttps://pub.dev/packages/qr_code_scanner
他们俩都在Flutter中使用平台视图

enter image description here

last_qr_scanner的完整示例代码

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

答案 1 :(得分:0)

您的相机视图必须是flutter小部件,才能嵌入到另一个小部件中。

您可以使用此程序包以抖动纹理的形式输出相机预览,并使用Mobile Vision API来检测QR码和条形码:https://github.com/rmtmckenzie/flutter_qr_mobile_vision