Opening a Website in the Browser with Flutter
Hello friends, in this post, I will explain how to navigate to a website from a Flutter app when a button is pressed.
The first thing we need to do is to add the plugin we will use in our application to the dependencies section of the pubspec.yaml file. "url_launcher: ^newversion"
dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.2
After adding it, we will now create a function, and when the user calls this function, it will open the site URL.
goToSite(opensiteurl) async {
var url = "https://" + opensiteurl;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Site cannot be opened.. $url';
}
}
We created a function named goToSite and when we use this function, the website will automatically open via the current, that is, the default browser.
If I need to give an example of how to use it:
RaisedButton(
onPressed: () {
goToSite("www.urhoba.blogspot.com");
},
child: Text(
"Open Website",
),
)
That's how easy it is to use, friends! Now, when you click this button, the desired website will open in the default browser.




Yorum Gönder