GlassMorph class Composition

A piece of glass that flows to the size of whatever child it is given.

Swap the child for one of a different identity — another type, or the same type with another Key, exactly the rule AnimatedSwitcher follows — and the glass grows or shrinks to the new child while the old content fades out and the new fades in. The way a button becomes its menu in iOS 26:

Align(
  alignment: Alignment.topRight, // holds the corner the morph holds
  child: GlassMorph(
    alignment: Alignment.topRight,
    borderRadius: isOpen ? const BorderRadius.all(Radius.circular(24)) : kGlassCapsule,
    child: isOpen
        ? MenuPanel(key: const ValueKey<String>('menu'), onClose: () => setState(() => isOpen = false))
        : IconButton(
            key: const ValueKey<String>('button'),
            onPressed: () => setState(() => isOpen = true),
            icon: const Icon(Icons.more_horiz),
          ),
  ),
)

A child of the same identity is updated in place, as in any widget: no morph, and the glass takes its new size at once. A change of width, height or borderRadius alone does morph, with nothing to cross-fade.

No size is typed. The glass measures the child: each one is laid out against the morph's own constraints, loosened, and the glass is that size — which is why width and height are overrides and not inputs. Each pins one axis: the child is laid out at exactly that width or height, the glass is that wide or tall, and the other axis is still measured.

One frame of measurement. The new child is laid out for the first time on the frame it is swapped in, and that frame draws the morph at its start — the old glass, the new content transparent — so the motion is visible from the next frame. Nothing is ever drawn at a size that was not measured.

Alignment: what holds still

alignment is the point of the glass that does not move relative to the morph's own box while the glass changes size. Alignment.topRight keeps the top-right corners of the old and new glass together and grows the rest down and to the left; Alignment.center grows evenly about the middle; Alignment.centerLeft holds the left edge's midpoint.

The morph's box is itself the glass's current size, placed by its parent — as AnimatedSize's is — so what holds still on the screen is decided by both, and this is the mistake that gets made: a morph inside a Center grows from its centre whatever alignment says, because Center moves the box to keep it centred. To hold a corner on screen, put the morph where its parent holds the same corner — Align(alignment: Alignment.topRight), a Positioned with top and right, the end of a Row — and give it the same alignment. The bud is placed by alignment too, so a mismatch also sends the neck the wrong way.

The default, Alignment.center, is the one that cannot surprise a parent that centres; a morph from a control anchored in a corner wants that corner.

The picture

Mid-morph the glass is two shapes in a GlassGroup at spacing: the body, which follows the spring from the old size to the new, and a bud — a capsule held at the same alignment, at the size the body is about to have — running ahead of it. Where the bud leads, the fold pulls the body after it: a rounded front with a waist where it meets the body's straighter edges, which closes as the body catches up. The bud is held at least the blend radius inside its own outline, so it never bulges past the held corner and is invisible by the time the body arrives.

Only a morph that grows on some axis has a bud; one that shrinks on both is the body receding, with the cross-fade — the new shape is inside the old one, and there is nothing for glass to run ahead into.

Content is clipped to the body, not to the bud: the glass leads, the content follows the size. The old content fades out over the first half of the spring's progress and the new fades in over the second.

A swap mid-morph starts again from where the glass is: the body from its current size and radius, the content from its current opacity, an earlier bud fading out from where it was. Nothing jumps, but the spring's velocity is not carried: the new morph starts from rest. Up to kMaxFusedShapes shapes can be in flight — the body and eleven buds of swaps in quick succession; the oldest are dropped past that, which is the one way to make the outline jump.

When there is no neck

The union is drawn only when it can be, and otherwise the morph is the body alone — a plain resize with the cross-fade, which is what AnimatedContainer would draw:

  • for a morph that shrinks on both axes (above);
  • at spacing zero, which is how to ask for that on purpose;
  • below GlassTier.full, where a group does not fuse and two overlapping flat panels would tint their overlap twice;
  • while the platform asks for reduced motion (MediaQuery.disableAnimationsOf) — and then there is no motion at all: the new child and its size arrive on the frame they are swapped in.

What it costs

At rest, a lone GlassSurface: no group, no fused draw, no capture of its own beyond any surface's. The group exists only while the glass moves.

While it moves, a capture per frame, and that is the honest price. The body changes size on every frame of the spring, so the capture's input changes on every frame — the same price as animating GlassSurface.materialize, and the reason the motion is short. The fused draw adds about 0.030 GPU cycles per device pixel per shape over the pixels it covers on Adreno 830 (see GlassGroup) — two shapes, while the morph lasts, plus the capture's own reach for the bridge (k times the fold's depression, a quarter of k for two shapes).

Forming the union is a few captures of its own whatever is declared — the swap, the bud joining the register, the layer watch settling on the group that now holds the body: the first five frames, measured, after which a declared region (below) holds.

A GlassTravel directly around the morph buys nothing: its region is its own box, which here is the morph's box and changes with it. One around an ancestor of fixed size that holds every size the morph takes — a slot in a toolbar, a SizedBox in a corner of a Stack — is a region that does not move, and then the morph moves on the proxy already held, as a slider's drop does; the content the morph covers must not repaint for that to hold (see GlassTravel). The bud and the overshoot must stay inside the region.

Limits

One level of glass: the morph is not a GlassAbove, nor is it meant to morph across levels — a bar becoming a sheet that stands on other glass is two widgets. It is its own group, so it cannot share a silhouette with neighbours in an enclosing GlassGroup while it moves; at rest it is a member of that group like any surface. Content under an Opacity mid-fade is composited through a layer, which glass inside the content does not survive — the content is meant to be labels and icons. Hit testing follows the body: mid-morph only the new child is hit, and only where the body has reached.

See also:

Inheritance

Constructors

GlassMorph({required Widget child, AlignmentGeometry alignment = Alignment.center, double? width, double? height, BorderRadius borderRadius = const BorderRadius.all(Radius.circular(24)), GlassMorphMotion motion = GlassMorphMotion.fluid, double spacing = kGlassMorphSpacing, GlassFinish? finish, bool labelled = true, VoidCallback? onEnd, Key? key})
Glass that holds child and flows to the size of each new one.
const

Properties

alignment → AlignmentGeometry
The point of the glass that holds still in the morph's box while the size changes. Read the class's section on it: on screen, the parent has to hold the same point.
final
borderRadius → BorderRadius
The corner radii of the glass around this child; a change morphs. kGlassCapsule makes a pill at any size, and a morph between a capsule and a panel moves the radius with the size.
final
child → Widget
What the glass holds, and what it measures. A child of another identity (Widget.canUpdate false) starts a morph.
final
finish → GlassFinish?
The optics, or null for the host's.
final
hashCode → int
The hash code for this object.
no setterinherited
height → double?
Pins the glass's height, and lays the child out at exactly it. Null measures the child.
final
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
labelled → bool
See GlassSurface.labelled.
final
motion → GlassMorphMotion
The spring. GlassMorphMotion.fluid by default; GlassMorphMotion.calm for large panels.
final
onEnd → VoidCallback?
Called when a morph has settled and the glass is a lone surface again.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
spacing → double
The GlassGroup.spacing of the union while the glass moves: how far the neck reaches. Zero morphs without a union — the body alone, no neck, no group even mid-morph.
final
width → double?
Pins the glass's width, and lays the child out at exactly it. Null measures the child.
final

Methods

createElement() → StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → State<GlassMorph>
Creates the mutable state for this widget at a given location in the tree.
override
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
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
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 short, textual description of this widget.
inherited

Operators

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