You can add a link to your website in Flutter by using the TextButton
widget. This widget allows you to create a clickable button that, when pressed, will open the specified URL in a web browser.
Steps to Add a Link:
-
Import the necessary package:
import 'package:flutter/material.dart';
-
Create a
TextButton
widget:TextButton( onPressed: () { // Open the website URL in a web browser launch('https://www.yourwebsite.com'); }, child: const Text('Visit Our Website'), )
-
Replace
https://www.yourwebsite.com
with your actual website URL.
Example:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart'; // Import the url_launcher package
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Link Example'),
),
body: Center(
child: TextButton(
onPressed: () async {
// Launch the website URL
final Uri url = Uri.parse('https://www.example.com');
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
throw 'Could not launch $url';
}
},
child: const Text('Visit Example Website'),
),
),
);
}
}
Note: You need to add the url_launcher
package to your project to use the launchUrl
function.
Additional Tips:
- You can customize the appearance of the
TextButton
widget by using properties likestyle
,padding
, andtextColor
. - You can use other widgets like
ElevatedButton
orOutlinedButton
to create different styles of clickable buttons. - You can also use the
launch
function from theurl_launcher
package to open other types of URLs, such as email addresses, phone numbers, or maps.