What is Align in Flutter?
Align is a widget that aligns its child within itself. It can optionally size itself based on the child's size. It's useful when you want to position a widget somewhere within a parent without using Stack and Positioned.
Basic Usage
Wrap any widget with Align and provide an alignment property. The default alignment is Alignment.center.
Alignment Options
You can use predefined constants from Alignment class:
Alignment.topLeftAlignment.topCenterAlignment.topRightAlignment.centerLeftAlignment.centerAlignment.centerRightAlignment.bottomLeftAlignment.bottomCenterAlignment.bottomRight
You can also create custom alignments using Alignment(x, y) where x and y range from -1 to 1 (e.g., Alignment(0.5, -0.5)).
WidthFactor and HeightFactor
By default, Align sizes itself to match its parent. But you can use widthFactor and heightFactor to size Align to a multiple of the child's size. For example, setting widthFactor: 2.0 makes Align twice as wide as its child.
Common Use Cases
- Positioning an icon in a corner of a container.
- Creating a badge overlay without using Stack.
- Centering a widget within a specific area.
Key Points to Remember
- Align places its child according to
alignment. - If no
widthFactor/heightFactorare given, Align takes the size of its parent. Alignmentuses a coordinate system where (0,0) is center, (-1,-1) is top‑left, (1,1) is bottom‑right.