3

I am trying to make layout of app where main container will have 45% of device height and container inside main container should be of fix size.

I have written following code but inner container takes full height as its parent(main) container.

class TestContainer extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
          width: 50,
          height:MediaQuery.of(context).size.height * 0.45,
          color: Colors.red
          child:Container(
          width: 50,
          height: 100,
          color: Colors.green
          )
        );
      }
    }

Any clue what I am doing wrong with this ?

3
  • Add a child to the second container. Commented Aug 7, 2020 at 18:48
  • Eg, Container (child: Text ("hello"),), Commented Aug 7, 2020 at 18:48
  • 1
    This won't fix the issue, you need to understand how constraints works in Flutter. flutter.dev/docs/development/ui/layout/constraints Commented Aug 7, 2020 at 18:53

3 Answers 3

7

Fix it by wrapping it with a Center widget:

class TestContainer extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
          width: 50,
          height:MediaQuery.of(context).size.height * 0.45,
          color: Colors.red,
          // wrap with a center widget
          child:Center(
            child: Container(
            width: 50,
            height: 100,
            color: Colors.green
            ),
          )
        );
      }
    }

TO PLACE THE INNER CONTAINER AT THE BOTTOM CENTER

class TestContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 50,
      height: MediaQuery.of(context).size.height * 0.45,
      color: Colors.red,
      // wrap with a align widget
      child: Align(
        // set the alignment property
        alignment: Alignment.bottomCenter,
        child: Container(
          width: 50,
          height: 100,
          color: Colors.green,
        ),
      ),
    );
  }
}

Read more about Layout Constraints in Flutter here: https://flutter.dev/docs/development/ui/layout/constraints

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Derek. Is there any way to position inner container to be bottom of main container. I tried alignment: Alignment.bottomCenter, but this seems to be not working.
You need to wrap the inner Container with an Align widget rather than using the alignment property. I updated my answer
1

Flutter Container's basic property is to consume the parents size... So in order to add Container as child to an another Container you simply nee to specify where you need the inner container to be displayed on outer container.. otherwise the child container will grow to the size of its parent Container.

Container(
    width: 400.0,
    height: 400.0,
    color: Colors.green,
    alignment: Alignment.center, // align your child's position.
    child: Container(
      width: 100.0,
      height: 100.0,
      color: Colors.red,
    ),
  ),

Comments

0
class TestContainer extends StatelessWidget {
        @override
     Widget build(BuildContext context) {
       return Container(
      width: 50,
      height:MediaQuery.of(context).size.height * 0.45,
      color: Colors.red,
      child:Stack(
  children: <Widget>[
      Positioned(
        bottom: 0,
        child: Container(
                width: 50,
                height: 100,
                color: Colors.green
                )
             )
           ] )
      ); }
}

1 Comment

I agree but there is multiple way to do same task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.