3

I'm trying to display image inside a container with responsive width and height.

Here is my code:

 ...
 child: Column (
    mainAxisSize: MainAxisSize.min,
    children: [
       Row (
          children: [
             Padding (
                padding: EdgeInsets.all(20),
                child: Container(
                   height: MediaQuery.of(context).size.height *.3,
                   width: MediaQuery.of(context).size.width * 1,
                   padding: const EdgeInsets.all(15),
                   decoration: BoxDecoration(
                      image: DecorationImage(
                         image: AssetImage("assets/images/gunung.jpg"),
                         fit: BoxFit.cover,
                ...

here is the output:

enter image description here

Is there any solutions?

3
  • hmmm i suggest to use a package for putting sizes so that u wont worry on to it package like flutter utils or sizer it auto the sizes on each orientation you want. But if you dont want it try layout builder if the screen is in land you can put the own size on it. Commented Feb 3, 2022 at 3:00
  • I've tried using Sizer but I got an error saying "lateinitializationerror field 'height' has not been initialized" Commented Feb 3, 2022 at 3:02
  • @ArbiterChil You don't need to use a package for basics of Flutter. Using a package simply increases your app dependency and size when you don't need one. Commented Feb 3, 2022 at 3:17

2 Answers 2

6

Just Change this to your code :

//Add Exapanded widget


 child: Column (
        mainAxisSize: MainAxisSize.min,
        children: [
           Row (
              children: [
    //Add Expanded Widget it will solve your issue 
    Expanded(
                 Padding (
                    padding: EdgeInsets.all(20),
                    child: Container(
                       height: MediaQuery.of(context).size.height *.3,
                       width: MediaQuery.of(context).size.width * 1,
                       padding: const EdgeInsets.all(15),
                       decoration: BoxDecoration(
                          image: DecorationImage(
                             image: AssetImage("assets/images/gunung.jpg"),
                             fit: BoxFit.cover,
Sign up to request clarification or add additional context in comments.

1 Comment

this works, thank you
0

Since you've set the width of your Container as

MediaQuery.of(context).size.width * 1

This means that it'll take up the width space of your device screen.

Not to mention, you've also set the Padding too. And that is taking up left, top, right and bottom spacing of 20.

padding: EdgeInsets.all(20)

This Padding is taking your Container ahead than it needs to go, hence the overflow.

There are many ways to fix this.

  1. You could subtract the padding from the width that you've initialized for your container. 20 from the left and 20 from the right.
(MediaQuery.of(context).size.width * 1) - 20 - 20
  1. You could set the padding within the container itself. You probably don't need Padding widget separately hanging around above Container.
    Padding(                                  // <---- remove
      padding: EdgeInsets.all(20),            // <---- remove
      child: Container(
        height: MediaQuery.of(context).size.height *.3,
        width: MediaQuery.of(context).size.width * 1,
        padding: const EdgeInsets.all(20),    // <---- use this
        ...

1 Comment

what about the padding around the container? tried using margin but it overflows

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.