How to open a web browser (URL) from my Flutter code?

I am creating a Flutter application and want to open the URL in a web browser or browser window (in response to a button click). How can i do this?

+30
source share
6 answers

Now it is implemented as a plugin.

https://pub.dartlang.org/packages/url_launcher

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'; } } 

Special symbols:

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

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

+58
source

[NOTE: UrlLauncher no longer exists. See the accepted answer for a solution.]

You can use UrlLauncher to do something like this:

 new FlatButton( textColor: style.mainTheme.accentColor, child: new Text(signInLabel, style: style.largeText), onPressed: () { UrlLauncher.launch('http://www.webpage.com'); } ); 
+6
source

If you want to use url_launcher than please use it in this form

 environment: sdk: ">=2.1.0 <3.0.0" dependencies: url_launcher: ^5.0.2 flutter: sdk: flutter 

This answer is also for absolute beginners: they think behind the SDK flutter. No, it was a failure. The packages were extras, not in the flutter of KFOR. These were secondary packages (single, small framework helpers).

+1
source

After some searching, this problem can be solved using the instructions listed here: https://groups.google.com/forum/#!topic/flutter-dev/J3ujgdOuG98

The above UrlLauncher no longer used.

0
source

You might want to use the webview plugin.

There is a community plugin with some limitations -

https://pub.dartlang.org/packages/flutter_webview_plugin

And there is an official -

https://github.com/flutter/plugins/tree/master/packages/webview_flutter

0
source

For flutter:

As described above by Gunter Zochbauer

For Flutter Web:

 import 'dart:html' as html; 

Then use:

 html.window.open(url, name); 

Make sure you run flutter clean if import not allowed.

0
source

Source: https://habr.com/ru/post/1266186/


All Articles