flutter - 扫描二维码后自动打开链接flutter
问题描述
我有一个颤振应用程序,我添加了一个二维码扫描程序包,以便我可以使用它。扫描二维码后如何自动打开链接。我使用了这个包qr_code_scanner。
这是我的代码实现
Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 220.0
: 300.0;
// To ensure the Scanner view is properly sizes after rotation
// we need to listen for Flutter SizeChanged notification and update controller
return QRView(
key: qrKey,
onQRViewCreated: onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea),
onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
);
}
onQRViewCreated(QRViewController qrViewController) {
// this.qrViewController = qrViewController;
qrViewController.scannedDataStream.listen((qrData) {
qrViewController.pauseCamera();
final String qrCode = qrData.code;
Get.to(DocumentDetails())!.then(
(value) => qrViewController.resumeCamera(),
);
});
}
void _onPermissionSet(BuildContext context, QRViewController ctrl, bool p) {
log('${DateTime.now().toIso8601String()}_onPermissionSet $p');
if (!p) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('no Permission')),
);
}
成功扫描二维码后,我已经实现了开辟一条新路线。我想知道如何在扫描各自的二维码后打开链接,即使它在 webview 中也是如此。
解决方案
从二维码读取链接后,使用 url_launcher 打开链接:
void _launchURL() async =>
await canLaunch(_url) ? await launch(_url) : throw 'Could not launch $_url';
推荐阅读
- javascript - 有没有一种方法可以在没有 ID 的情况下获取 SELECT TAG 中的所有选项值?
- postman - Postman 从文件中获取变量
- python - 如何修复“python 版本总是返回 2.7 而不是 3.7”
- oracle - 不同时间段的 Obiee 列度量值相同
- javascript - 将普通数组转换为对象数组
- android - Android 工作室有没有办法轮询光传感器而不是等待 SensorEvent?
- android - 退款客户应用内购买,但 BillingClient 仍然显示用户已购买?
- android - 自定义分隔线在线性布局中不起作用
- datatable - 行数=“10”的extendedDataTable 中的侦听器问题
- javascript - 防止选择中间有无效日期的日期范围