flutter web用url_launcher跳转被safari拦截

flutter web用url_launcher跳转被safari拦截

暂时不知道什么原因, 有的链接又可以正常跳转

解决方案

页面内点击跳转按钮时, 先增加一个确认弹窗, 点击确认之后再跳转

/// 弹窗封装
/// flutter1版本, 用了bot_toast: ^2.3.0插件  
showAlertDialog({
    @required String title,
    BackButtonBehavior backButtonBehavior = BackButtonBehavior.ignore,
    VoidCallback cancel,
    VoidCallback confirm,
    int fontsize,
    bool isConstraint = false,
    VoidCallback backgroundReturn,
  }) {
    BotToast.showAnimationWidget(
      clickClose: !isConstraint,
      allowClick: false,
      backButtonBehavior: backButtonBehavior,
      wrapToastAnimation: (controller, cancel, child) => Stack(
        children: <Widget>[
          GestureDetector(
            onTap: () {
              if (!isConstraint) {
                cancel();
                backgroundReturn?.call();
              }
            },
            child: AnimatedBuilder(
              builder: (_, child) => Opacity(
                opacity: controller.value,
                child: child,
              ),
              child: DecoratedBox(
                decoration: BoxDecoration(color: Colors.black26),
                child: SizedBox.expand(),
              ),
              animation: controller,
            ),
          ),
          CustomOffsetAnimation(
            controller: controller,
            child: child,
          )
        ],
      ),
      toastBuilder: (cancelFunc) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
        title: Text(
          title,
          style: TextStyle(fontSize: fontsize ?? 16),
        ),
        actions: <Widget>[
          isConstraint
              ? Container()
              : FlatButton(
                  onPressed: () {
                    cancelFunc();
                    cancel?.call();
                  },
                  highlightColor: const Color(0x55FF8A80),
                  splashColor: const Color(0x99FF8A80),
                  child: const Text(
                    '取消',
                    style: TextStyle(color: Colors.redAccent),
                  ),
                ),
          FlatButton(
            onPressed: () {
              cancelFunc();
              confirm?.call();
            },
            child: const Text('确定'),
          ),
        ],
      ),
      animationDuration: Duration(milliseconds: 300),
    );
  }

使用

/// 方法1: 使用url_launcher: ^5.4.1
/// 优点: 打包方便, 用途多
/// 缺点: 引入插件
showAlertDialog(
        title: '即将跳转第三方游戏',
        confirm: () => urlLauncher(launchUrl: launchUrl),
      );

Future urlLauncher({@required String launchUrl}) async {
    if (await canLaunch(launchUrl)) {
      await launch(
        launchUrl,
        forceSafariVC: false,
      );
    } else {
      throw 'Could not launch $launchUrl';
    }
  }
/// 方法2: 使用本地 'dart:html'库
/// 优点: 直接使用本地自带的方法
/// 缺点: 打包app时需要隐藏, 否则报错(app不支持该html库)
import 'dart:html' as html;

showAlertDialog(
        title: '即将跳转第三方游戏',
        confirm: () => html.window.open(launchUrl, '第三方游戏'),
      );
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容