使用Flutter套接字的错误SSL证书警报对话框

时间:2019-04-01 16:16:14

标签: ssl flutter ssl-certificate

在检测到无效证书时是否可以显示警报对话框?我可以很容易地连接起来,只需从badCert函数中调整“ true”即可,但是我想显示一个警告对话框,并根据响应返回true或false。

SecureSocket.connect("<HOST>", <PORT>,onBadCertificate: badCert).then((Socket sock) {

      socket = sock;

      socket.listen(dataHandler,
          onError: errorHandler,
          onDone: doneHandler,
          cancelOnError: false);
      //socket.write(sb.toString());
    }).catchError(errorHandler);

bool badCert(X509Certificate cert) {
    _asyncConfirmDialog(ctx).then((action) {
      if (action == ConfirmAction.ACCEPT) {
        return true;
      }
      return false;
    });
    return false;
  }

Future<ConfirmAction> _asyncConfirmDialog(BuildContext context) async {
  return showDialog<ConfirmAction>(
    context: context,
    barrierDismissible: false, // user must tap button for close dialog!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Certificate Issue'),
        content: const Text(
            'You have an invalid or error with you SSL Certificate.  Do you want to Accept?'),
        actions: <Widget>[
          FlatButton(
            child: const Text('Cancel'),
            onPressed: () {
              Navigator.of(context).pop(ConfirmAction.CANCEL);
            },
          ),
          FlatButton(
            child: const Text('Accept'),
            onPressed: () {
              Navigator.of(context).pop(ConfirmAction.ACCEPT);
            },
          )
        ],
      );
    },
  );
}

0 个答案:

没有答案