How to fit an Image to column width in Flutter?

5,863

Solution 1

I suggest the following chenges

Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Image.network("Your image url",
          width: double.infinity, 
          fit: BoxFit.fitWidth,
        ),
        Text("Fitted image"),
      ],
    )

Solution 2

Well, I resolved my problem using MediaQuery.

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Container(
        child: Image.network("Some image url", fit: BoxFit.fill,), 
        width: MediaQuery.of(context).size.width,
    ),
    Text("Fitted image"),
  ],
),
Share:
5,863
José Lozano Hernández
Author by

José Lozano Hernández

Updated on December 16, 2022

Comments

  • José Lozano Hernández
    José Lozano Hernández over 1 year

    I want to fit a child image to the width of the column father.

    Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Image.network("Some image url"),
        Text("Fitted image"),
      ],
    ),
    
  • José Lozano Hernández
    José Lozano Hernández over 4 years
    Your solution is not bad, but Expand expands through the main axis, so it will grow vertically (column), it works because the image keeps his aspect ratio and grows his width also ;-)
  • José Lozano Hernández
    José Lozano Hernández about 4 years
    Perfect, this answer is even better!