如何将 barcode_scan 小部件用作其他小部件的子部件?

How to use barcode_scan widget as a child to some other widget?

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

您可以使用包 https://pub.dev/packages/last_qr_scanner or https://pub.dev/packages/qr_code_scanner
他们都在 Flutter

中使用平台视图

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

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

您可以使用此包在颤振纹理上输出相机预览,并使用移动视觉 API 检测 QR 码和条形码:https://github.com/rmtmckenzie/flutter_qr_mobile_vision