flutter 扫描二维码 qr_code_scanner黑屏问题

原因:
软键盘弹出的时候 跳转qr_code_scanner 插件扫描二维码页面会出现黑屏问题

解决办法:
1.使用TextFormField时 跳转按钮不要写在 suffixIcon或者prefixIcon里面 单独提出来不要和TextFormField关联
跳转时隐藏软键盘

 FocusScope.of(context).unfocus();
 Future.delayed(Duration(milliseconds: 100), () async {
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return QRViewPage();
      })).then((value) {
        print('scan_____${value}');
      });
 });

2.暴力解决:
界面打开时直接重启照相机,会出现不流畅的情况

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

class QRViewPage extends StatefulWidget {
  @override
  _QRViewPageState createState() => _QRViewPageState();
}

class _QRViewPageState extends State {
  Barcode result;
  QRViewController controller;
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');



  @override
  Widget build(BuildContext context) {

    var scanArea = (MediaQuery.of(context).size.width < 400 ||
        MediaQuery.of(context).size.height < 400)
        ? 200.0
        : 300.0;
    print("扫描大小=$scanArea");
    print("width=${MediaQuery.of(context).size.width}");

    return Scaffold(
        body: SafeArea(  // add safe area here
        child:QRView(
          key: qrKey,
          // You can choose between CameraFacing.front or CameraFacing.back. Defaults to CameraFacing.back
          // cameraFacing: CameraFacing.front,
          onQRViewCreated: _onQRViewCreated,
          // Choose formats you want to scan. Defaults to all formats.
          // formatsAllowed: [BarcodeFormat.qrcode],
          overlay: QrScannerOverlayShape(
            borderColor: Colors.red,
            borderRadius: 10,
            borderLength: 30,
            borderWidth: 10,
            cutOutSize: scanArea,
          ),
        )));
  }

  void _onQRViewCreated(QRViewController controller) {
    print("controller====");
    this.controller = controller;
    String qrData;
    controller.scannedDataStream.listen((scanData) {
      print("scanData====$scanData");
      if (scanData != null) {
        qrData = scanData.code;
        controller.dispose();
      }
    }, onDone: () {
      Navigator.of(context).pop(qrData);
    });
    refresh();
  }

  void refresh() {
    Future.delayed(Duration(milliseconds: 100), () async {
      print("刷新相机");
      await controller.pauseCamera();
      await controller.resumeCamera();
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}

你可能感兴趣的:(flutter 扫描二维码 qr_code_scanner黑屏问题)