问题
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
来源:https://stackoverflow.com/questions/56512036/how-do-i-link-http-url-to-flutter-web