【问题标题】:How to use barcode_scan widget as a child to some other widget?如何将barcode_scan 小部件用作其他小部件的子部件?
【发布时间】:2019-10-30 06:55:46
【问题描述】:

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

【问题讨论】:

    标签: android flutter barcode barcode-scanner


    【解决方案1】:

    您可以使用包https://pub.dev/packages/last_qr_scannerhttps://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,
                )
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多