The Center
widget centers a widget horizontally and vertically in the center of a Stack. How can one center a widget ONLY horizontally or vertically?
Centering a widget in a Stack is possible with a Row or Column widget. Can it be done without using those widgets?
Conditions:
- Cannot use Row / Column.
- The size of the Stack is not known before layout (cannot use hardcoded Positioned values).
Center widget:
Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.black87, width: 10)),
child: Stack(
children: [
Center(
child: Container(color: Colors.amber, width: 50, height: 50),
),
],
),
);
Center horizontally w/Row:
Row(mainAxisAlignment: MainAxisAlignment.center)
Center vertically w/Column:
Column(mainAxisAlignment: MainAxisAlignment.center)
The alignment above is the desired result. Can those results be achieved with other Flutter widgets?