gpt4 book ai didi

flutter - 如何在 Flutter 中扫描二维码后自动打开链接而不单击某些按钮

转载 作者:行者123 更新时间:2023-12-04 10:59:45 25 4
gpt4 key购买 nike

我有一个关于 Flutter 中二维码扫描的小问题。二维码读取数据成功后如何打开网址?

我用 this使用二维码的包,和this用于打开 URL,这是我的功能,用于检查来自 QR 码的数据值是否为 URL,如果是 URL,则运行该功能以打开网站。

checkingValue() {
if (_result != null || _result != "") {
if (_result.contains("https") || _result.contains("http")) {
return _launchURL(_result);
} else {
Toast.show("Invalide URL", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}
} else {
return null;
}
}

_launchURL(String urlQRCode) async {
String url = urlQRCode;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
_result变量是一个字符串,用于从二维码数据中获取值。

这是我的全部代码:
class _ScannerPageState extends State<ScannerPage> {

String _password;
String _result = "";

Future _scanQR() async {
try {
String qrResult = await BarcodeScanner.scan();
setState(() {
_result = qrResult;
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
_result = "Camera permission was denied";
Toast.show(_result, context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
});
} else {
setState(() {
_result = "Unknown Error $ex";
Toast.show(_result, context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
});
}
} on FormatException {
setState(() {
_result = "You pressed the back button before scanning anything";
Toast.show(_result, context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
});
} catch (ex) {
setState(() {
_result = "Unknown Error $ex";
Toast.show(_result, context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
});
}
}

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
return showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return PopUp(
content: "Are you sure want to exit?",
cancelText: "No",
acceptText: "Yes",
onTapCancel: () => Navigator.of(context).pop(),
onTapAccept: () async {
await SessionManager().removeSession();//
await SystemNavigator.pop();
},
);
}
);
},
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.lock),
onPressed: () {
Navigator.pushNamed(context, '/login');
},
),
],
),
body: Column(
children: <Widget>[
Text(_result.contains("https") || _result.contains("http") ? _result : "Invalid URL"),
],
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.camera_alt),
label: Text("Scan"),
onPressed: () => _scanQR(),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
),
);
}

checkingValue() {
if (_result != null || _result != "") {
if (_result.contains("https") || _result.contains("http")) {
return _launchURL(_result);
} else {
Toast.show("Invalide URL", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}
} else {
return null;
}
}

_launchURL(String urlQRCode) async {
String url = urlQRCode;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}

}

所以,我把 checkingValue() 放在哪里扫描二维码后运行它的功能?

最佳答案

通行证qrResultcheckingValue方法

Future _scanQR() async {
try {
String qrResult = await BarcodeScanner.scan();
checkingValue(qrResult);
//....
}
checkingValue方法
checkingValue(String url) {
//...
}

或调用 checkingValue()
setState(() {_result = qrResult;});
checkingValue();
//...

关于flutter - 如何在 Flutter 中扫描二维码后自动打开链接而不单击某些按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58898382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com