flutter_linkify 2.1.0

flutter_linkify pub package

Turns text URLs and emails into clickable inline links in text for Flutter.

Pub - API Docs - GitHub

Install #

Install by adding this package to your pubspec.yaml:

dependencies:
  flutter_linkify: ^2.1.0

It is highly recommend that you also add a dependency on url_launcher to open links in the browser/OS.

Usage #

Basic:

import 'package:flutter_linkify/flutter_linkify.dart';

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

Styling #

Add a style to non-links (yellow) or links (red), and open in browser using url_launcher:

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

Linkify(
  onOpen: (link) async {
    if (await canLaunch(link.url)) {
        await launch(link.url);
      } else {
        throw 'Could not launch $link';
      }
  },
  text: "Made by https://cretezy.com",
  style: TextStyle(color: Colors.yellow),
  linkStyle: TextStyle(color: Colors.red),
);

Humanizing #

Remove http:// or https:// from the start of the URL using humanize:

Linkify(
  text: "Made by https://cretezy.com",
  humanize: true,
);

Advance #

In the onOpen callback, a LinkableElement is passed in. You can check if it is a LinkElement (URL) or EmailElement using is for custom handling.

You can enable parsing of only some link types using the linkTypes option. All are enabled by default.

Full example can be found at example/lib/main.dart.

Example Screenshot

[2.1.0] - 2019-03-23

  • Split to linkify package.
    • This might break if you were using the internal linkify method.
  • Add text alignment (#11).

[2.0.0] - 2019-03-17

  • Add email address linking (thanks PieterAelse! #8).
  • Breaking: Change API to pass a LinkableElement to the onOpen callback:
    • Accessing the URL can be done by using link.url.
    • You can check using is if it is a LinkElement or EmailElement for custom handling.
  • Added linkTypes option to enable/disable parsing URLs/emails.
  • More line break fixes (should be the last! #9).

[1.1.1] - 2019-03-03

  • Fix humanize option on multi-link text (not being passed down).

[1.1.0] - 2019-02-19

  • Add humanize option to remove protocol.

[1.0.4] - 2019-02-19

  • Fix bug with newlines (#1)

[1.0.3] - 2018-11-06

  • Fix another bug with whitespace (#1)

[1.0.2] - 2018-11-05

  • Fix bug with whitespace (#1)

[1.0.1] - 2018-09-15

  • Add more documentation.

[1.0.0+1] - 2018-09-15

  • Update description.

[1.0.0] - 2018-09-15

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'dart:async';

import 'package:url_launcher/url_launcher.dart';

void main() => runApp(new LinkifyExample());

class LinkifyExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'flutter_linkify example',
      home: Scaffold(
        appBar: AppBar(
          title: Text('flutter_linkify example'),
        ),
        body: Center(
          child: Linkify(
            onOpen: _onOpen,
            text: "Made by https://cretezy.com\n\nMail: example@gmail.com",
          ),
        ),
      ),
    );
  }

  Future<void> _onOpen(LinkableElement link) async {
    if (await canLaunch(link.url)) {
      await launch(link.url);
    } else {
      throw 'Could not launch $link';
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_linkify: ^2.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_linkify/flutter_linkify.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
94
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
linkify ^1.0.0 1.0.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
pedantic ^1.0.0