Home > Software design >  Open the link in the browser in flutter_linkify package
Open the link in the browser in flutter_linkify package

Time:09-22

I use this package to open links within the text, but this package opens the link inside the application, can I make it open the link in the browser?

This is the package link: https://pub.dev/packages/flutter_linkify

CodePudding user response:

You can use the following example to open a link in a browser

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

CodePudding user response:

In the README of flutter_linkify it is clearly written that

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

Install

Install by adding this package to your pubspec.yaml:

dependencies:
  flutter_linkify: ^5.0.2

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

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),
);
  • Related