Skip to content
Talk to an engineerRequest access

Link

Markdown links, reference links and automatic links — and what happens when one is tapped.

A paragraph with a titled link, then bare URLs, a www host and an email address, all drawn as links

[label](url "title"), [label](<url with spaces>), and reference links [label][ref] with a [ref]: url line. Bare URLs, www. hosts, email addresses and <https://…> are linked automatically — inline syntax covers schemes and turning it off.

Links do nothing on their own: the package does not depend on a URL launcher, so opening one is your decision.

GptMarkdown(
reply,
onLinkTap: (url, title) => launchUrlString(url),
)
styleSheet: const GptMarkdownStyleSheet(
link: LinkStyle(
color: Color(0xFF0B57D0),
hoverColor: Color(0xFF0842A0),
decoration: TextDecoration.none,
fontWeight: FontWeight.w500,
),
),
GptMarkdown(text, onLinkTap: (url, title) => launchUrlString(url))

title is the label text, which is useful for confirmation dialogs:

onLinkTap: (url, title) async {
final ok = await confirm('Open "$title"?\n$url');
if (ok) await launchUrlString(url);
},

How a link is drawn.

Set it as GptMarkdownStyleSheet(link: LinkStyle(…)) on the widget, or in GptMarkdownThemeData for the whole app. Every field is optional; an unset one falls back to the theme, then to the default.

Field Type Description
color Color? Link colour. Defaults to the theme’s linkColor.
hoverColor Color? Colour under the pointer. Defaults to linkHoverColor.
decoration TextDecoration? Defaults to an underline.
decorationThickness double? Underline thickness. Defaults to the font’s.
fontWeight FontWeight? Weight of link text. Defaults to the surrounding weight.
Member Type Description
merge(LinkStyle? other) LinkStyle This style, with any unset field taken from other, field by field.
resolve(ColorScheme scheme) LinkStyle This style with every remaining default filled in.
copyWith({…}) LinkStyle A copy with the given fields replaced.
static lerp(LinkStyle? a, LinkStyle? b, double t) LinkStyle? Linearly interpolates between two styles.
resolve: more

Colours fall back to the theme’s linkColor and linkHoverColor, which remain the app-wide way to set them.

Builds the span for a link, replacing the default rendering.

Wins over linkBuilder when both are set. Returning a span rather than a widget keeps the link on the text baseline, wrapping across lines and selectable — none of which a WidgetSpan can do.

typedef InlineLinkBuilder = InlineSpan Function(LinkBuildDetails details);

The callback function to handle link clicks.

void Function(String url, String title)? onLinkTap

Whether bare URLs, www. hosts, email addresses and <...> autolinks become links. Defaults to true.

Bare autolinks follow the GFM autolink extension; <...> autolinks follow CommonMark §6.5. Turn this off to render URLs as plain text — for untrusted input where an accidental tap target is unwelcome.

bool autolink

Extra URL schemes linked without <>.

http, https, mailto and xmpp are always linked. Anything else has to be opted into, because a bare foo://bar in prose is usually not meant as a link:

GptMarkdown(text, autolinkSchemes: const {'myapp'})

This does not affect <...> autolinks, which accept any scheme as CommonMark specifies — the author wrote the brackets deliberately.

Set<String> autolinkSchemes

Whether to follow the link color.

bool followLinkColor

Builds a widget for a link.

Used only when inlineLinkBuilder is null. The result is wrapped in a WidgetSpan, which is the shape that made this hook a problem — prefer inlineLinkBuilder, or styleSheet’s LinkStyle when you only want to restyle.

typedef LinkBuilder = Widget Function(
BuildContext context,
InlineSpan text,
String url,
TextStyle style,
);

What the link builder is told about one [label](url) or autolink.

Member Type Description
context BuildContext The element this construct is being built in.
config GptMarkdownConfig The configuration in force at this point in the document.
style TextStyle The text style in effect for this construct, fully resolved.
url String The link target — /docs/a, https://x.dev, mailto:[email protected]. As written, except that backslash escapes and entity references are decoded (CommonMark), and a reference link ([text][label]) carries the URL of its [label]: url definition. Never otherwise resolved or normalised.
label String The label as plain text.
labelSpans List<InlineSpan> The label, already parsed and styled with style.
linkStyle LinkStyle The resolved LinkStyle — colour, hover colour, decoration, thickness and weight, with GptMarkdownTheme fallbacks already applied.
isAutolink bool Whether the package found this as a bare URL rather than as […](…).
onTap VoidCallback? Invokes GptMarkdownConfig.onLinkTap for this link, or null when no handler is set.
hoverStyle TextStyle style recoloured with LinkStyle.hoverColor, for TappableTextSpan.hoverStyle.
defaultSpan({TextStyle? style, List<InlineSpan>? children}) InlineSpan Exactly what a link renders as when no builder is given.
asWidgetSpan(…) InlineSpan child in a placeholder wrapped exactly the way the deprecated LinkBuilder’s result is, with onTap attached.
config: more

Read GptMarkdownConfig.scope from it to tell a link in a table cell from one in ordinary content, and GptMarkdownConfig.textDirection or GptMarkdownConfig.textScaler when a widget of your own needs them. TableBuilder and OrderedListBuilder already take a whole config for the same reason.

style: more

A builder never has to guess a default: this is what the construct would have been drawn with, its own style already applied over the surrounding one.

label: more

This is what GptMarkdownConfig.onLinkTap receives as its second argument. For an autolink it is the URL itself. It is not a link title: the parser reads [text](url "title") titles into MdLink.title, but they do not reach builders yet.

labelSpans: more

The parse has happened by the time a builder runs, in the MarkdownScope.linkLabel scope, so [**bold** link](url) arrives as the spans that render it. An autolink’s label is one plain run, deliberately: re-parsing a URL would let ItalicMd eat the underscores out of https://example.com/a_b_c.

linkStyle: more

Passed alongside style so a builder can reach a value a TextStyle cannot carry. hoverColor is the one that exists today.

onTap: more

Hand it to TappableTextSpan.onTap — or to defaultSpan, which does it for you — rather than calling onLinkTap yourself, so the arguments stay right when they change.

defaultSpan: more

Returning this from InlineLinkBuilder is a no-op, which makes it the safe starting point: keep it, then change one thing.

style replaces the style on the container span. It does not restyle the label, because labelSpans were already built with their own styles — bold inside a link stays bold, and a colour set here does not reach them. To recolour the label, rebuild it and pass children:

inlineLinkBuilder: (link) => link.defaultSpan(
children: [TextSpan(text: link.label, style: link.style.copyWith(
color: link.url.startsWith('https://') ? null : Colors.orange,
))],
),

For a widget instead of a span, see asWidgetSpan.

asWidgetSpan: more

The escape hatch for a link that genuinely has to be a widget. It cannot wrap across lines, is skipped by text selection, is one opaque character to the streaming reveal, and does not paint on iOS when nested inside another placeholder — prefer a span where the design allows.