I'm learning flutter web.
I'm trying to open another url when button is clicked.
Is there any way like this:
onclick: ()=>openurl("https://test.com")
How can I achieve this?
Please help
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Current easiest way to do it is by using href
with your html
library:
import 'dart:html' as html;
html.window.location.href = "https://www.google.com" // or any website your want
Put that code inside your onTap
method and that's it.
回答2:
Flutter Web does not support plugins (yet), so you have to use replacements from dart:html
https://api.dartlang.org/stable/2.4.0/dart-html/Window/open.html
window.open(url, 'tab');
or
https://api.dartlang.org/stable/2.4.0/dart-html/Window/location.html window.location.assign(url);
回答3:
There is a plugin to achieve this: https://pub.dartlang.org/packages/url_launcher
Import the plugin;
import 'package:url_launcher/url_launcher.dart';
openURL() async {
const url = 'https://test.com';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Call the method