Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Column CrossAxisAlignment.stretch cause the Column to grow?

I have a layout like this:

Container(
  alignment: Alignment.center,
  color: Colors.blueGrey,
  child: Container(
    color: Colors.amber,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          width: 150, // Dummy - actual width is unknown
          height: 100,
          color: Colors.red,
        ),
        Container(
          width: 300, // Dummy - actual width is unknown
          height: 100,
          color: Colors.green,
        )
      ],
    ),
  ),
)

Which produces this layout:

Current state

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then I add crossAxisAlignment: CrossAxisAlignment.stretch, to the Column, expecting this:

expected

However, what actually happens is this:

actual

Why? How can I achieve my expected result?

>Solution :

stretchwill try to get as much space as possible. You can set the yellow Container width and use stretch but this is hard-coded. Also you use IntrinsicWidth over Column widget and it will use max child width , but it has some cost.

Container(
  alignment: Alignment.center,
  color: Colors.blueGrey,
  child: IntrinsicWidth(
    child: Container(
      color: Colors.amber,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 150, // Dummy - actual width is unknown
            height: 100,
            color: Colors.red,
          ),
          Container(
            width: 300, // Dummy - actual width is unknown
            height: 100,
            color: Colors.green,
          )
        ],
      ),
    ),
  ),
),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading