Flutter web:在 url_launcher 的 target="_self" 中启动页面

Flutter web: Launch page in target="_self" with url_launcher

我正在使用 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';
}

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

有办法吗?谢谢

任何其他在移动设备和网络中打开 url 的解决方案,并且能够在自身中打开网络 link 的可能性也被接受

在 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';
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更新:2021 年 1 月 12 日 此信息记录在 api 文档 here