Text motion

Text that changes in place keeps what it can. Words keep the letters they share, numbers roll digit by digit. Nothing swaps, so the eye never loses its place.

Words morph. Shared letters travel, new ones fade in. Press the button: its own label changes the same way.

Numbers roll. Each digit rolls where it stands, up when the value grows and down when it falls. Press the price.

Which one

ChangeUseWhy
A label on the same control: Save to Saved, Buy to BuyingTextMorphthe shared letters stay, so it reads as one label changing state
A value in place: a price, a percentage, a balanceRolleach digit rolls where it stands, up for more and down for less
A whole sentence, or text in a different placeneither: a 150ms crossfadethere is nothing shared to follow
Text the user is typingRoll, anchored at the starteach key adds a glyph on the right and the rest must not move

How a word morphs

RuleValue
Matchinga letter is told apart by what it is and which one of its kind it is: the second o stays the second o
Shared lettersglide to their new place on a 400ms spring with no bounce
New lettersfade in from 80% size over the same 400ms
Letters that are gonefade out to 80% in 120ms, faster than the rest arrives
Where it livesinside the word. If the button itself moves, the letters move with it and do not trail behind
Widththe word's width travels at the same pace, so a button around it grows or shrinks with the letters instead of snapping first
Every buttona label that changes on the same Button morphs by itself; nothing to set
Screen readershear the whole new text once, never letter by letter
Reduced motionthe letters change at once

In Flutter

The widget measures where every character of the new string starts, keeps the letters both strings share and moves them there, and fades the rest. It is the effect of Torph (torph.lochie.me), written for Flutter.

Dart
class TextMorph extends StatefulWidget {
  const TextMorph(this.text, {super.key, required this.style, this.duration = const Duration(milliseconds: 400)});
  final String text;
  final TextStyle style;
  final Duration duration;
  @override
  State<TextMorph> createState() => _TextMorphState();
}

class _Glyph {
  _Glyph(this.key, this.char, this.x, {this.fresh = true});
  final String key;        // the character and which one of its kind it is: "o:0", "o:1"
  final String char;
  double x;
  bool fresh;              // new in this string: fades in
  bool leaving = false;    // gone from this string: fades out, then is dropped
}

class _TextMorphState extends State<TextMorph> {
  List<_Glyph> glyphs = [];
  Size box = Size.zero;

  @override
  void initState() {
    super.initState();
    _measure(first: true);
  }

  @override
  void didUpdateWidget(TextMorph old) {
    super.didUpdateWidget(old);
    if (old.text != widget.text || old.style != widget.style) setState(_measure);
  }

  void _measure({bool first = false}) {
    // lay the whole string out once, so every letter keeps its kerning
    final painter = TextPainter(
      text: TextSpan(text: widget.text, style: widget.style),
      textDirection: TextDirection.ltr,
      maxLines: 1,
    )..layout();
    final before = {for (final g in glyphs) if (!g.leaving) g.key: g};
    final seen = <String, int>{};
    final next = <_Glyph>[];
    var offset = 0;
    for (final c in widget.text.characters) {
      final n = seen.update(c, (v) => v + 1, ifAbsent: () => 0);
      final x = painter.getOffsetForCaret(TextPosition(offset: offset), Rect.zero).dx;
      final kept = before.remove('$c:$n');
      next.add(kept == null ? _Glyph('$c:$n', c, x, fresh: !first) : (kept..x = x..fresh = false));
      offset += c.length;
    }
    // what is left in "before" is gone from the text: it stays where it was while it fades
    glyphs = [...next, for (final g in before.values) g..leaving = true];
    box = painter.size;
  }

  @override
  Widget build(BuildContext context) {
    final still = MediaQuery.disableAnimationsOf(context);
    final move = still ? Duration.zero : widget.duration;
    return Semantics(
      label: widget.text,
      excludeSemantics: true,
      child: AnimatedContainer(
        duration: move,
        curve: MoonshotMotion.easeOut,
        width: box.width,
        height: box.height,
        child: Stack(clipBehavior: Clip.none, children: [
          for (final g in glyphs)
            AnimatedPositioned(
              key: ValueKey(g.key),
              duration: move,
              curve: MoonshotMotion.easeOut,
              left: g.x,
              top: 0,
              child: TweenAnimationBuilder<double>(
                tween: Tween(begin: g.fresh ? 0 : 1, end: g.leaving ? 0 : 1),
                duration: still ? Duration.zero : (g.leaving ? const Duration(milliseconds: 120) : widget.duration),
                curve: MoonshotMotion.easeOut,
                onEnd: () { if (g.leaving && mounted) setState(() => glyphs.remove(g)); },
                builder: (_, t, child) => Opacity(opacity: t, child: Transform.scale(scale: .8 + .2 * t, child: child)),
                child: Text(g.char, style: widget.style),
              ),
            ),
        ]),
      ),
    );
  }
}

// Use it wherever a label changes on the same control:
FilledButton(onPressed: save, child: TextMorph(saved ? 'Saved' : 'Save', style: MoonshotType.labelButton))

For numbers, Roll is the other half: one AnimatedSwitcher per digit, keyed by its place counted from the right, sliding up when the value grows and down when it falls, with tabular figures so nothing shifts sideways.