I have just started learning flutter, and I cannot figure out why a Spacer() will not work in the following structure between the 2 Texts, shouldnt it create space vertically between the 2 Text so that the height of the column is the same as the height of the circular avatar?
import 'package:flutter/material.dart';
class TestSpacer extends StatelessWidget {
const TestSpacer({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const Column(
children: [
Row(
children: [
CircleAvatar(
radius: 48,
backgroundColor: Colors.white,
child: Icon(Icons.person),
),
Column(
children: [
Text("123"),
// Spacer(),
Text("456"),
],
)
],
)
],
),
);
}
}
I tried putting expandables around the column that wraps the text, and still the spacer doesnt work as expected.
2
Answers
The
Spacer
widget fills the available space within the column.Lets wrap the
column
with a red coloredcontainer
to troubleshoot.You will see that the size of the column is small, leaving no space for the
Spacer()
to expand. This is because it has no parent widget that determines the height and width of the Column.If we wrap them into an
Expanded
widget, it only expands horizontally.So your only solution is to specify the
height
parameter of theContainer
(orSizedBox
).Instead of hard coding it, you can make the height match the CircleAvatar in two ways.
Label a variable:
Incase CircleAvatar is not the only widget you are trying to align with, you can use the
LayoutBuilder
widget to get the height constraint of the whole Row.Hope this will help u,