Creating Clickable Links in Flutter with the flutter_linkify Package

Aug 26, 2022 | Programming

In the world of Flutter development, making text URLs and emails clickable can elevate user experience significantly. By utilizing the flutter_linkify package, you can seamlessly transform plain text into interactive links. This guide will take you through the steps involved in installation, usage, and a few tricks to make the most of this highly useful package, ensuring your app shines with its links!

Installation Guide

To get started, you need to add flutter_linkify to your Flutter project. Here’s how you do it:

dependencies:
  flutter_linkify: ^6.0.0

It is highly recommended to also include a dependency on the url_launcher package, which allows you to open links in the user’s default browser.

Basic Usage

Once you’ve installed the package, you can use it as follows:

import 'package:flutter_linkify/flutter_linkify.dart';

Linkify(
  onOpen: (link) => print('Clicked ${link.url}!'),
  text: 'Made by https://cretezy.com',
);

In this example, when a link is clicked, a simple message is printed to the console. However, this is just the tip of the iceberg!

Styling Links

You can add some flair to your links through styling. Here’s how:

import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:url_launcher/url_launcher.dart';

Linkify(
  onOpen: (link) async {
    if (!await launchUrl(Uri.parse(link.url))) {
      throw Exception('Could not launch ${link.url}');
    }
  },
  text: 'Made by https://cretezy.com',
  style: TextStyle(color: Colors.yellow), // Non-links styled
  linkStyle: TextStyle(color: Colors.red), // Links styled
);

In this snippet, non-link text is colored yellow while links are highlighted in red. You can also utilize entire RichText options to enhance the text’s appearance.

Linkify Options

By default, flutter_linkify humanizes URLs by removing the “http://” or “https://” prefixes. To disable this feature, you can customize the LinkifyOptions:

Linkify(
  text: 'Made by https://cretezy.com',
  options: LinkifyOptions(humanize: false),
);

Selecting Text

If you want users to be able to select links easily, consider using the SelectableLinkify widget:

SelectableLinkify(
  text: 'Made by https://cretezy.com\n\nMail: example@gmail.com',
);

Advanced Features

In more complex applications, the onOpen callback provides access to a LinkableElement. This element can be checked to see if it’s a URL or email, allowing for custom handling:

onOpen: (link) {
  if (link is UrlElement) {
    // Handle URL
  } else if (link is EmailElement) {
    // Handle email
  }
}

You are also able to specify which types of links to parse by using the linkifiers option, giving you much greater control over how links behave in your application.

Troubleshooting

If you encounter any issues while implementing the flutter_linkify package, here are some troubleshooting tips:

  • Ensure you’ve added the dependencies correctly in your pubspec.yaml file.
  • Check for any errors in your console when trying to launch URLs. Make sure the URLs are in the correct format.
  • Verify if the url_launcher package is also included, as it’s crucial for opening links.
  • If you cannot find a feature or functionality, refer to the API Docs for comprehensive documentation.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With the flutter_linkify package, turning plain text URLs and emails into interactive links is a breeze. By following the steps outlined in this guide, you can easily implement and style clickable links in your Flutter applications, enhancing user experience and functionality.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox