Flutter How to link a website to a Text widget?

Viewed 884

I have Text on the menu, when clicked, a specific link opened in my phone browser.

Padding(
      padding: const EdgeInsets.only(left:20.0),
      child: GestureDetector(
        onTap: (){
          //for example link https:google.com
        },
        child:Text(
          "Rate the app",
          style: TextStyle(
              fontSize: 18.0
          ),
        ),
      ),

    ),

2 Answers

You can use the url_launcher.


Padding(
      padding: const EdgeInsets.only(left:20.0),
      child: GestureDetector(
        onTap: () => _launchURL(), // call the _launchURL here
        child:Text(
          "Rate the app",
          style: TextStyle(
              fontSize: 18.0
          ),
        ),
      ),

    ),

_launchURL method like this:


_launchURL() async {
    const url = 'https://google.com';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

Dependencies:

url_launcher: latestVersion

import 'package:url_launcher/url_launcher.dart';

MaterialButton(
               shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20))),
                      color: Colors.white,
                      child: Text(
                        "StackOverflow",
                        style: TextStyle(color: Colors.pink),
                      ),
                      onPressed: () async {
                        final url =
                            "https://stackoverflow.com/users/5137539/anil-kumar?tab=profile";
                        if (await canLaunch(url)) {
                          await launch(
                            url,
                          );
                        }
                      }),
Related