sliver_text

Pub Version License: MIT pub points

SliverText is to Text what SliverList is to ListView: a run of text that goes straight into the slivers list of a CustomScrollView, with no SliverToBoxAdapter wrapper.

It behaves like Text — same parameters, same inherited configuration — and adds a few things a sliver makes easy.

Installation

dependencies:
  sliver_text: ^1.0.0

Requires Dart 3.8 / Flutter 3.32 or newer.

Usage

CustomScrollView(
  slivers: <Widget>[
    const SliverAppBar(title: Text('sliver_text')),
    const SliverText('A paragraph that scrolls with everything else.'),
    SliverPadding(
      padding: const EdgeInsets.all(16),
      sliver: const SliverText(
        'Padded, wrapped, ellipsized after three lines.',
        maxLines: 3,
        overflow: TextOverflow.ellipsis,
      ),
    ),
  ],
)

Rich text

SliverText.rich(
  TextSpan(
    text: 'Mixed ',
    children: <InlineSpan>[
      const TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(
        text: ' and tappable',
        style: const TextStyle(decoration: TextDecoration.underline),
        recognizer: TapGestureRecognizer()..onTap = _open,
      ),
    ],
  ),
)

WidgetSpan is not supported: a widget span needs a box child, which a sliver has nowhere to put. Use SliverToBoxAdapter(child: Text.rich(...)) for that.

Gradient

A gradient painted through the glyphs, without a ShaderMask box in the way:

const SliverText(
  'Gradient headline',
  style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
  gradient: LinearGradient(colors: <Color>[Colors.indigo, Colors.pink]),
)

The gradient spans the painted bounds of the glyphs, not the box around them: a sliver forces its child to the full cross axis extent, so a shader stretched over that box would spend most of its range on empty space.

Selection

Inside a SelectionArea, SliverText registers itself like Text does, so the text can be selected and copied.

Semantics

The text is exposed to screen readers automatically. semanticsLabel replaces what is spoken, for content that does not read well:

const SliverText(r'$99', semanticsLabel: 'ninety nine dollars')

Parameters

SliverText takes the same parameters as Text:

Parameter Notes
style Merged onto the ambient DefaultTextStyle unless inherit: false.
strutStyle, textAlign, textDirection, locale As in Text. textDirection defaults to the ambient Directionality.
softWrap, overflow, maxLines As in Text. overflow defaults to TextOverflow.clip.
textScaler Defaults to the user's setting from MediaQuery.
textWidthBasis, textHeightBehavior As in Text.
selectionColor Used inside a SelectionArea.
semanticsLabel Replaces the spoken text.
gradient Extra: paints the glyphs through a gradient.

SliverRichText is the low-level widget underneath, the sliver equivalent of RichText; RenderSliverText is its render object.

Migrating from 0.0.2

  • Long text now wraps. overflow used to default to TextOverflow.ellipsis, which collapsed anything longer than one line into a single ellipsized line. It now defaults to TextOverflow.clip, like Text. Pass overflow: TextOverflow.ellipsis together with maxLines to get the old look deliberately.
  • direction is now textDirection and defaults to the ambient Directionality instead of always being left-to-right.
  • style is now nullable and merges with the ambient DefaultTextStyle rather than defaulting to an empty TextStyle.
  • SliverTextAdapter is now RenderSliverText. The old name still works as a deprecated alias.

License

MIT.

Libraries

sliver_text
Text rendered directly as a sliver, without a SliverToBoxAdapter.