Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

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

I am using url_launcher package.

When in Flutter web, I would like the url to be opened in the current page, and not in target="_blank"

I tried adding forceWebView: true,

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

And also added headers thinking they might have something to do, but they don't.

Is there a way to do this? Thank you

Any other solution to open a url in mobile and in web, and that enables the possiblity to open the web link in self is accepted also


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In flutter_web if you want to achieve this you can use the webOnlyWindowName property and pass _self or _blank depending on your choice.

  • _self - opens in the same tab.
  • _blank - opens in a new tab.

I am not sure if its documented properly. But you can find the piece of code responsible for this here.

Following is a working solution which you can test.

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

Update: 12.01.2021 This information is documented in the api documentation here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...