Skip to content

Instantly share code, notes, and snippets.

@ffkev
Created August 27, 2024 06:20
Show Gist options
  • Save ffkev/f67fde01d5df536cbc052f87ccccb610 to your computer and use it in GitHub Desktop.
Save ffkev/f67fde01d5df536cbc052f87ccccb610 to your computer and use it in GitHub Desktop.
Launch a website in the same tab in Flutter web
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