LinkSpan class

A WidgetSpan that represents a link with hover effects and tap handling.

It can be used to create clickable text spans with a hover style and optional background decoration. This allows easy and clean implementation of links within rich text widgets.

The LinkSpan widget provides various customization options such as text style, hoverStyle, background decoration, padding, animation duration, and curve. It also supports optional prefix and suffix widgets that can be displayed before and after the text respectively.

Example usage:

Text.rich(
  TextSpan(
    text: 'By signing up, you agree to our ',
    children: [
      LinkSpan(
        onTap: () => launchUrlString('https://example.com/terms'),
        text: 'Terms of Service',
        style: TextStyle(color: Colors.blue,
        hoverStyle: TextStyle(decoration: TextDecoration.underline),
      ),
      TextSpan(text: ' and '),
      LinkSpan(
      onTap: () => launchUrlString('https://example.com/privacy'),
      text: 'Privacy Policy',
      style: TextStyle(color: Colors.blue),
      hoverStyle: TextStyle(decoration: TextDecoration.underline),
    ),
  ],
),

This will create a text span with two clickable links that change style on hover.

Inheritance

Constructors

LinkSpan({TextStyle? style, TextStyle? hoverStyle, VoidCallback? onTap, required String text, Decoration? background, Decoration? hoverBackground, MouseCursor? cursor, Color? color, Color? hoverColor, EdgeInsets? padding, Duration duration = Duration.zero, Curve curve = Curves.linear, PlaceholderAlignment alignment = PlaceholderAlignment.baseline, TextBaseline? baseline = TextBaseline.alphabetic, Widget? prefix, Widget? suffix})
Creates a LinkSpan with the given parameters.
const

Properties

alignment PlaceholderAlignment
How the placeholder aligns vertically with the text.
finalinherited
background Decoration?
Optional background decoration for the link span.
final
baseline TextBaseline?
The TextBaseline to align against when using ui.PlaceholderAlignment.baseline, ui.PlaceholderAlignment.aboveBaseline, and ui.PlaceholderAlignment.belowBaseline.
finalinherited
child Widget
The widget to embed inline within text.
no setteroverride
color Color?
Foreground color of the text and/or icon in the link span when not hovered.
final
cursor MouseCursor?
The cursor to display when hovering over the link span.
final
curve Curve
The curve of the hover animation.
final
duration Duration
The duration of the hover animation.
final
hashCode int
The hash code for this object.
no setterinherited
hoverBackground Decoration?
Optional background decoration for the link span in hovered state.
final
hoverColor Color?
Foreground color of the text and/or icon in the link span when hovered.
final
hoverStyle TextStyle?
The style to apply to the text when not hovering.
final
onTap VoidCallback?
Callback function that is called when the link is tapped.
final
padding EdgeInsets?
The padding around the link span.
final
prefix Widget?
An optional widget to display before the text. This can be used to add an icon or any other widget before the text.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
style TextStyle?
The TextStyle to apply to this span.
finalinherited
suffix Widget?
An optional widget to display after the text. This can be used to add an icon or any other widget after the text.
final
text String
The text to display in the link span.
final

Methods

build(ParagraphBuilder builder, {TextScaler textScaler = TextScaler.noScaling, List<PlaceholderDimensions>? dimensions}) → void
Adds a placeholder box to the paragraph builder if a size has been calculated for the widget.
inherited
codeUnitAt(int index) int?
Returns the UTF-16 code unit at the given index in the flattened string.
inherited
codeUnitAtVisitor(int index, Accumulator offset) int?
Performs the check at each InlineSpan for if the index falls within the range of the span and returns the corresponding code unit. Returns null otherwise.
inherited
compareTo(InlineSpan other) RenderComparison
Describe the difference between this span and another, in terms of how much damage it will make to the rendering. The comparison is deep.
inherited
computeSemanticsInformation(List<InlineSpanSemanticsInformation> collector) → void
Walks the InlineSpan tree and accumulates a list of InlineSpanSemanticsInformation objects.
inherited
computeToPlainText(StringBuffer buffer, {bool includeSemanticsLabels = true, bool includePlaceholders = true}) → void
PlaceholderSpans are flattened to a 0xFFFC object replacement character in the plain text representation when includePlaceholders is true.
inherited
debugAssertIsValid() bool
In debug mode, throws an exception if the object is not in a valid configuration. Otherwise, returns true.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
getSemanticsInformation() List<InlineSpanSemanticsInformation>
Flattens the InlineSpan tree to a list of InlineSpanSemanticsInformation objects.
inherited
getSpanForPosition(TextPosition position) InlineSpan?
Returns the text span that contains the given position in the text.
inherited
getSpanForPositionVisitor(TextPosition position, Accumulator offset) InlineSpan?
Performs the check at each InlineSpan for if the position falls within the range of the span and returns the span if it does.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toPlainText({bool includeSemanticsLabels = true, bool includePlaceholders = true}) String
Flattens the InlineSpan tree into a single string.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A brief description of this object, usually just the runtimeType and the hashCode.
inherited
visitChildren(InlineSpanVisitor visitor) bool
Calls visitor on this WidgetSpan. There are no children spans to walk.
inherited
visitDirectChildren(InlineSpanVisitor visitor) bool
Calls visitor for each immediate child of this InlineSpan.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited