Skip to main content

Module center

Module center 

Source
Expand description

Center — a single-child wrapper that centers its child within the available space.

On a bounded axis (the parent proposes an exact size), Center fills that dimension and places the child in the middle. On an unbounded axis (the parent leaves it open, as a stack does on its main axis), Center shrink-wraps to the child’s natural size rather than collapsing to zero — this prevents the child from overflowing a prior sibling. Center always reports flex = 0, so it never claims slack from a stack’s distribution pass; to center content within leftover space, wrap it in an Expand: Expand::horizontal().child(Center::new().child(w)).

The child is measured under the constraint Center received (a loose-but-bounded proposal, like Flutter’s Center): rigid children keep their natural size and are centered, while adaptive children respond to the bound — an ellipsis TextWidget truncates at the slot width instead of overflowing symmetrically, and wrapping text reports its real wrapped height.

§When to use

  • Center a small widget inside a bounded slot (e.g., an icon in a fixed square cell).
  • Shrink-wrap and center an element inside a layout that provides an exact proposal in both axes.

For claiming all remaining stack space and then centering within it, use Expand wrapping Center instead.

// Center a rect in the full slot provided by its parent
let _centered = Center::new().child(RectWidget::new());

Structs§

Center
Centers a single child within the space this widget is given.