Robert Estivill
Robert Estivill

Reputation: 12477

Align widget to bottom of sibling column

I'm having trouble recreating the layout in the screenshot that follows two rules:

enter image description here

This is what I currently have

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Row(
          children: [
            Container(
              padding: const EdgeInsets.all(24),
              color: Colors.green,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    height: 500,
                    width: 200,
                    color: Colors.blue,
                  )
                ],
              ),
            ),
            Container(
              padding: const EdgeInsets.all(24),
              color: Colors.greenAccent,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    padding: const EdgeInsets.all(24),
                    height: 100,
                    width: 200,
                    color: Colors.yellow,
                  ),
                  Container(
                    padding: const EdgeInsets.all(24),
                    height: 100,
                    width: 200,
                    color: Colors.red,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

And it looks like this:

enter image description here

Now onto the things I have tried and the problems:

  1. Change right colum to mainAxisSize: MainAxisSize.max, results in the right column deciding the height.

enter image description here

  1. Add Spacer in between the yellow and red container results in the right column expanding to the max height.

enter image description here

So the question is: How can I constrain the height of the right column so

I have the feeling that I'm missing something or need a special widget that I'm not aware of. Maybe some advance usage of Flexible?

Any ideas or tips are more than welcome. Thanks.

========================

Full solution based on @Ivo Beckers answer.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Center(
          child: IntrinsicHeight(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.green,
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Container(
                          height: 500,
                          width: 200,
                          color: Colors.blue,
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.greenAccent,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 200,
                        width: 200,
                        color: Colors.yellow,
                      ),
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 50,
                        width: 200,
                        color: Colors.red,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

Upvotes: 4

Views: 1143

Answers (2)

Mano Haran
Mano Haran

Reputation: 1

try this code..

 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',

      home: Container(
        color: Colors.white,

        child: Row(
          children: [
            Flexible(child: Container(
              height: MediaQuery.of(context).size.height,
              padding: const EdgeInsets.all(24),
              color: Colors.green,
              child:  Container(
                height: 500,
                width: MediaQuery.of(context).size.width,
                color: Colors.blue,
              ),
            ),flex: 1,),
          const SizedBox(width: 2.0,),
          Flexible(child:   Container(
            height: MediaQuery.of(context).size.height,
            padding: const EdgeInsets.all(24),
            color: Colors.greenAccent,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  padding: const EdgeInsets.all(24),
                  height: 100,
                  width: 200,
                  color: Colors.yellow,
                ),
                Container(
                  padding: const EdgeInsets.all(24),
                  height: 50,
                  width: 200,
                  color: Colors.red,
                ),
              ],
            ),
          ),flex: 1,)
          ],
        ),
      ),
    );
  }

Upvotes: 0

Ivo
Ivo

Reputation: 23134

I got your example to work like this

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Align(
          child: IntrinsicHeight(
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.green,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        height: 500,
                        width: 200,
                        color: Colors.blue,
                      )
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.greenAccent,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 100,
                        width: 200,
                        color: Colors.yellow,
                      ),
                      const Spacer(),
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 100,
                        width: 200,
                        color: Colors.red,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

So basically the Row is wrapped in an IntrinsicHeight and that one in an Align. Just an IntrinsicHeight didn't seem to help. Furthermore a Spacer() between the Containers in the second Column

Upvotes: 2

Related Questions