What is Center in Flutter?
Center is a widget that centers its child within itself. It's actually a specialized version of Align with alignment: Alignment.center. It's one of the simplest and most commonly used layout widgets.
Basic Usage
The Center widget will expand to fill its parent (if the parent allows it) and then place the child in the center.
Center vs Align
Center is equivalent to Align(alignment: Alignment.center). Use Center for simplicity when you just need centering. Use Align when you need more precise positioning (e.g., top‑right).
When Does Center Not Work?
Center will only work if its parent provides enough constraints. For example, if Center is placed inside a Container with no fixed size, and that Container is inside a Column, Center might not have a bounded size to center within. In such cases, wrap the Center with a SizedBox or Expanded.
Key Points to Remember
- Center centers its child both horizontally and vertically.
- It expands to fill its parent (if possible).
- It's a convenient shorthand for
Align(alignment: Alignment.center).