一、生成二维码
1、导入依赖
在 pubspec.yaml 中 dependencies 节点下添加:
# 二维码生成 https://pub.dev/packages/qr_flutter
qr_flutter: ^3.2.0
2、引入代码
import 'package:qr_flutter/qr_flutter.dart';
3、属性
4、代码
Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(16.0))),
child: QrImage(
// backgroundColor: Colors.white,
foregroundColor: Colours.app_main,
data: shareData['share_url'] ?? '',
size: 200.0,
)),
二、扫描二维码
1、导入依赖
在 pubspec.yaml 中 dependencies 节点下添加:
# 二维码扫描 https://pub.dev/packages/barcode_scan
barcode_scan: ^3.0.1
2、引入代码
import 'package:barcode_scan/barcode_scan.dart';
3、代码
class _QRCodeState extends State<QRCode> {
var textStr = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("二维码"),
),
body: Container(
width: MediaQuery.of(context).size.width, //充满屏幕宽度,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center, //居中
children: [
SizedBox(height: 50,),
RaisedButton(
child: Text("二维码扫描"),
onPressed: () {
getQrcodeState().then((value) => setState(() {
this.textStr = value;
}));
},
),
SizedBox(height: 20,),
Text("扫描内容为${this.textStr}"),
],
),
),
);
}
//扫描二维码
static Future<String> getQrcodeState() async {
try {
const ScanOptions options = ScanOptions(
strings: {
'cancel': '取消',
'flash_on': '开启闪光灯',
'flash_off': '关闭闪光灯',
},
);
final ScanResult result = await BarcodeScanner.scan(options: options);
return result.rawContent;
} catch (e) {
}
return null;
}
}