我正在尝试使用库qr_code_scanner在flutter中扫描qr代码。我不知道问题出在哪里,但是代码没有扫描,显然没有显示输出。我比较陌生。
代码是
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qr_code_scanner/qr_scanner_overlay_shape.dart';
void main()
{
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey qrKey= GlobalKey();
QRViewController controller;
String qrText='';
@override
// void initState(){
// super.initState();
// //FlutterMobileVision.start().then((x) => setState((){}));
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Barcode Scr"),
),
body: Column(
children: <Widget>[
// Padding(
// padding: const EdgeInsets.symmetric(vertical: 10.0),
// ),
Expanded(
flex: 5,
child: QRView(key: qrKey,
overlay: QrScannerOverlayShape(
borderRadius: 10,
borderColor: Colors.red,
borderLength: 30,
borderWidth: 10,
cutOutSize: 300,
) ,
onQRViewCreated: _onQRViewCreated,),
),
// RaisedButton(
// splashColor: Colors.pinkAccent,
// color: Colors.red,
// child: Text("Scan", style: TextStyle(
// fontSize: 20.0,
// color: Colors.white,
// ),),
// onPressed: Scan(),
// ),
// Padding(
// padding: const EdgeInsets.symmetric(vertical: 10.0),
// ),
Expanded(
flex: 1,
child: Center(
child: Text('Scan result: $qrText'
// , softWrap: true, style: TextStyle(
// fontSize: 20.0,
// color: Colors.black,),
),
),
),
],
),
)
);
}
// requestPermission() async{
// PermissionStatus result= await SimplePermissions.requestPermission(permission);
// }
// Scan() async{
// try{
// //String reader= await BarcodeScanner.scan();
// if(!mounted)
// {
// return;
// }
// setState(() => _reader= reader);
// }on PlatformException catch(e){
// if(e.code == BarcodeScanner.CameraAccessDenied){
// requestPermission();
// }
// else
// {
// _reader="unknown error";
// }
// }on FormatException catch(e){
// setState(() {
// _reader="not scanned";
// });
// }
// }
@override
void dispose(){
controller?.dispose();
super.dispose();
}
void _onQRViewCreated(QRViewController controller) {
this.controller=controller;
controller.scannedDataStream.listen((scanData) {
setState(){
qrText=scanData;
print(qrText);
}
}
);
}
}
答案 0 :(得分:0)
您需要调用 setState()
来更新 QRViewController
,并使用 Barcode.code
获取二维码的值,
void _onQRViewCreated(QRViewController controller) {
setState(() {
this.controller=controller;
});
controller.scannedDataStream.listen((Barcode scanData) {
setState((){
qrText = scanData.code;
print('$qrText');
});
});
}