📅  最后修改于: 2023-12-03 14:41:15.398000             🧑  作者: Mango
Flutter QR is a library built for Flutter developers that allows their apps to generate and scan QR codes with ease. It supports both QR code generation and scanning out of the box, making it a convenient tool to integrate into your Flutter projects.
Add the following line to your pubspec.yaml
file:
dependencies:
flutter_qr: ^0.1.0
Then run flutter packages get
in your terminal.
To generate a QR code with Flutter QR, use the QrImage
widget. Here's an example:
import 'package:flutter_qr/flutter_qr.dart';
import 'package:flutter/material.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: QrImage(
data: "https://www.google.com/",
version: QrVersions.auto,
size: 200.0,
gapless: false,
errorStateBuilder: (cxt, err) {
return Container(
child: Center(
child: Text(
"Uh oh! Something went wrong...",
textAlign: TextAlign.center,
),
),
color: Colors.white,
);
},
),
);
}
}
To scan a QR code with Flutter QR, use the QRView
widget. Here's an example:
import 'package:flutter_qr/flutter_qr.dart';
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey qrKey = GlobalKey();
QRViewController controller;
@override
Widget build(BuildContext context) {
return Scaffold(
body: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: 300,
),
),
);
}
void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
print(scanData);
});
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
}
For more information on how to use Flutter QR, please refer to the official documentation.