【问题标题】:Flutter web: Launch page in target="_self" with url_launcherFlutter web:使用 url_launcher 在 target="_self" 中启动页面
【发布时间】:2021-02-15 13:40:52
【问题描述】:

我正在使用url_launcher 包。

在 Flutter web 中,我希望 URL 在当前页面中打开,而不是在 target="_blank" 中打开

我尝试添加forceWebView: true,

if (await canLaunch(url)) {
  await launch(
    url,
    forceSafariVC: true,
    forceWebView: true,
    headers: <String, String>{'target': '_self'},
  );
} else {
  throw 'Could not launch $url';
}

还添加了标题,认为他们可能有事可做,但他们没有。

有没有办法做到这一点?谢谢

任何其他在移动和网络中打开 url 的解决方案,并且允许在自己中打开网络链接的可能性也被接受

【问题讨论】:

    标签: hyperlink flutter-web


    【解决方案1】:

    如果你想在flutter_web中实现这一点,你可以使用webOnlyWindowName属性并根据你的选择传递_self_blank

    • _self - 在同一个标​​签页中打开。
    • _blank - 在新标签页中打开。

    我不确定它的记录是否正确。但是您可以找到负责此here 的代码。

    以下是您可以测试的有效解决方案。

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() {
      runApp(UrlLauchDemo());
    }
    
    class UrlLauchDemo extends StatelessWidget {
      String url = 'https://www.google.com';
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  MaterialButton(
                    color: Colors.greenAccent,
                    child: Text('Launch Google in this page'),
                    onPressed: () async {
                      if (await canLaunch(url)) {
                        await launch(
                          url,
                          forceSafariVC: true,
                          forceWebView: true,
                          webOnlyWindowName: '_self',
                        );
                      } else {
                        throw 'Could not launch $url';
                      }
                    },
                  ),
                  SizedBox(
                    height: 100,
                  ),
                  MaterialButton(
                    color: Colors.blueAccent,
                    child: Text('Launch Google in new Tab'),
                    onPressed: () async {
                      if (await canLaunch(url)) {
                        await launch(
                          url,
                          forceSafariVC: true,
                          forceWebView: true,
                          webOnlyWindowName: '_blank',
                        );
                      } else {
                        throw 'Could not launch $url';
                      }
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    

    更新:12.01.2021 此信息记录在 api 文档here

    【讨论】:

    • 太棒了,这很有效,我必须等待 19 小时才能提供赏金
    猜你喜欢
    • 2013-03-24
    • 2021-04-29
    • 1970-01-01
    • 2021-07-12
    • 2020-04-16
    • 2022-10-17
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多