【问题标题】:How to open a link automatically after Scanning QR Code in Flutter without clicking some Button如何在 Flutter 中扫描二维码后自动打开链接而不单击某些按钮
【发布时间】:2020-03-12 20:49:16
【问题描述】:

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

我使用this 包来使用 QR 码,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() 扫描二维码后运行它的函数放在哪里?

【问题讨论】:

    标签: flutter qr-code


    【解决方案1】:

    qrResult 传递给checkingValue 方法

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

    checkingValue 方法

    checkingValue(String url) {
        //...
    }
    

    或在

    之后致电checkingValue()
    setState(() {_result = qrResult;});
    checkingValue();
    //...
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多