Created
August 27, 2024 06:20
-
-
Save ffkev/f67fde01d5df536cbc052f87ccccb610 to your computer and use it in GitHub Desktop.
Launch a website in the same tab in Flutter web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:url_launcher/url_launcher.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Web Example'), | |
), | |
body: Center( | |
child: ElevatedButton( | |
onPressed: () { | |
launchWebsite('https://www.example.com'); | |
}, | |
child: Text('Open Website'), | |
), | |
), | |
), | |
); | |
} | |
void launchWebsite(String url) async { | |
if (await canLaunch(url)) { | |
await launchUrl( | |
Uri.parse(url), | |
webOnlyWindowName: '_self', // Opens the URL in the same tab | |
); | |
} else { | |
throw 'Could not launch $url'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment