如果你想在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