Search code examples
flutterflutter-layout

Expanded widget not working as expected inside Columns and Rows


Note. this is just an example to simplify what I want to achieve.

I want to fill this space with a yellow container:

Before adding the container

So I made a container and wrap it with Expanded widget to fill the whole space and this is what I got:

the wrong output shape

the code:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
  decoration:
      BoxDecoration(border: Border.all(width: 5, color: Colors.black)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 300,
            height: 300,
            color: Colors.blue,
            alignment: Alignment.center,
            child: Text('300x300')),
          Column(mainAxisSize: MainAxisSize.min, children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              alignment: Alignment.center,
              child: Text('100x100')),
            Expanded(
              child: Container(width: 100, color: Colors.amber),
            )
            ],
          )
        ],
      ),
    );
  }

what I want to achieve is something like this: expected shape

the code of this shape:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
      decoration:
      BoxDecoration(border: Border.all(width: 5, color: Colors.black)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 300,
            height: 300,
            color: Colors.blue,
            alignment: Alignment.center,
            child: Text('300x300')),
          Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 100,
                height: 100,
                color: Colors.green,
                alignment: Alignment.center,
                child: Text('100x100')),
              Container(
                width: 100,
                height: 200,
                color: Colors.amber,
                alignment: Alignment.center,
                child: Text('100x200')),
            ],
          )
        ],
      ),
    );
  }

If I know the heights and widths so yes I can design this shape. but, what If I don't know the height of the blue Container?

I think it's a problem with Expanded that should be fixed.

Expanded widget should expand its child to the limit boundary of its parent, not expanding its parent too!!

Edited.

I will explain my actual problem to anyone who didn't understand my problem: enter image description here

I have this list of comments and what I want is that green line beside the comment fills the height to the end of the comment widget.

so I can't set a fixed size to the comment because I may have a comment with a long string so the line won't fill to the end of the comment text would exceed its limits.


Solution

  • Finally, I could solve it by using the IntrinsicHeight widget.

    I just wrap my Container with it and then the Expanded widget works as expected.

    the final UI:

    the final output

    Here is the code:

     @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: IntrinsicHeight(
            child: Container(
              decoration:
                  BoxDecoration(border: Border.all(width: 5, color: Colors.black)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                      width: 300,
                      height: 300,
                      color: Colors.blue,
                      alignment: Alignment.center,
                      child: Text('300x300')),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                          width: 100,
                          height: 100,
                          color: Colors.green,
                          alignment: Alignment.center,
                          child: Text('100x100')),
                      Expanded(
                        child: Container(width: 100, color: Colors.amber),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      }