Open In App

Flutter – URL Launcher

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This is basically a flutter plugin by which we can redirect the web page or you can say with this plugin we can open a link and redirect the website. Anywhere you want to go if you want to go on the email app then you just tap and you redirect the email.

How to Install It?

First, you need to go to the pub.dev website, and then you have to search for the URL launcher in the search box. Then you get much more plugins by this name but you choose only those that have a high percentage and more use by the developer.

 

After that, you just found five options readme, changelog, example, installing, versions, and scores.

 

How to install the URL launcher plugin?

For installing the plugin we have two methods

Method 1: You get the line in the install section you just copy the line and paste it on your terminal command and run it

flutter pub add url_launcher

 

Method 2: Simply copy the given dependencies and paste them into your project pubsec.yaml under the Cupertino icon and run the flutter pub get in your terminal.

url_launcher: ^6.1.10

 

Copy from here and paste it into your project as you can see in the image.

 

Note: Always use the latest version of the dependencies.

 After that, you simply copy this line and paste it into your home section.

import 'package:url_launcher/url_launcher.dart';

 

Copy from here and paste it into your project as you can see in the images.

 

Example of URL Launcher

Dart




// ignore_for_file: prefer_const_constructors
 
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
 
class UrlLink extends StatefulWidget {
  const UrlLink({super.key});
 
  @override
  State<UrlLink> createState() => _UrlLinkState();
}
 
class _UrlLinkState extends State<UrlLink> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Geeks For Geeks"),
      ),
      body: Column(
        // crossAxisAlignment: CrossAxisAlignment.start,
        // ignore: prefer_const_literals_to_create_immutables
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: () async {
              const url = 'https://www.geeksforgeeks.org/';
              if (await canLaunch(url)) {
                await launch(url, forceWebView: true, enableJavaScript: true);
              } else {
                throw 'Could not launch $url';
              }
            },
            child: Center(
              child: Text(
                "Want Read Article Tap Here",
                style: TextStyle(
                    color: Colors.green,
                    fontSize: 25,
                    fontWeight: FontWeight.w400),
              ),
            ),
          ),
        ],
      ),
    );
  }
}


Output

Explanation

Here we are adding the link using the URL Launcher which is a plugin provided by the pub.dev whole process we write here you can follow and also implement it. We are adding the GeeksforGeeks website link on the text as you can see in the video so you can also add it according to your need and project.
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads