如何从我的 Flutter 代码打开 Web 浏览器 (URL)? [英] How do I open a web browser (URL) from my Flutter code?

查看:138
本文介绍了如何从我的 Flutter 代码打开 Web 浏览器 (URL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Flutter 应用程序,我想在网络浏览器或浏览器窗口中打开一个 URL(响应点击按钮).我该怎么做?

解决方案

TL;DR

现在实现为插件

const url = "https://flutter.io";如果(等待 canLaunch(url))等待启动(网址);别的//无法启动 url,有一些错误throw "无法启动 $url";


完整示例:

<块引用>

import 'package:flutter/material.dart';导入'包:url_launcher/url_launcher.dart';无效主(){运行应用程序(新脚手架(身体:新中心(孩子:新的凸起按钮(onPressed: _launchURL,child: new Text('显示 Flutter 主页'),),),));}_launchURL() 异步 {const url = 'https://flutter.io';如果(等待 canLaunch(网址)){等待启动(网址);} 别的 {throw '无法启动 $url';}}

在 pubspec.yaml 中

依赖项:url_launcher: ^5.7.10

特殊字符:

如果 url 值包含现在 URL 中允许的空格或其他值,请使用

Uri.encodeFull(urlString)Uri.encodeComponent(urlString) 并传递结果值.

I am building a Flutter app, and I'd like to open a URL into a web browser or browser window (in response to a button tap). How can I do this?

解决方案

TL;DR

This is now implemented as Plugin

const url = "https://flutter.io";
if (await canLaunch(url))
  await launch(url);
else 
  // can't launch url, there is some error
  throw "Could not launch $url";


Full example:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(new Scaffold(
    body: new Center(
      child: new RaisedButton(
        onPressed: _launchURL,
        child: new Text('Show Flutter homepage'),
      ),
    ),
  ));
}

_launchURL() async {
  const url = 'https://flutter.io';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

In pubspec.yaml

dependencies:
  url_launcher: ^5.7.10

Special Characters:

If the url value contains spaces or other values that are now allowed in URLs, use

Uri.encodeFull(urlString) or Uri.encodeComponent(urlString) and pass the resulting value instead.

这篇关于如何从我的 Flutter 代码打开 Web 浏览器 (URL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆