Flutter how to handle image with fixed size inside box? Flutter how to handle image with fixed size inside box? flutter flutter

Flutter how to handle image with fixed size inside box?


You need to add - crossAxisAlignment: CrossAxisAlignment.stretch, in Column so that children can take up horizontal space.

Working Code:

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    final title = 'MyApp';return MaterialApp(  title: title,  home: Scaffold(    appBar: AppBar(      title: Text(title),    ),    body: ListView(      children: <Widget>[        Container(          margin:EdgeInsets.all(8.0),          child: Card(            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),            child: InkWell(              onTap: () => print("ciao"),              child: Column(                crossAxisAlignment: CrossAxisAlignment.stretch,  // add this                children: <Widget>[                  ClipRRect(                    borderRadius: BorderRadius.only(                      topLeft: Radius.circular(8.0),                      topRight: Radius.circular(8.0),                    ),                    child: Image.network(                        'https://placeimg.com/640/480/any',                       // width: 300,                        height: 150,                        fit:BoxFit.fill                    ),                  ),                  ListTile(                    title: Text('Pub 1'),                    subtitle: Text('Location 1'),                  ),                ],              ),            ),          ),        ),        Container(          margin:EdgeInsets.all(8.0),          child: Card(            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),            child: InkWell(              onTap: () => print("ciao"),              child: Column(                crossAxisAlignment: CrossAxisAlignment.stretch,                children: <Widget>[                  ClipRRect(                    borderRadius: BorderRadius.only(                      topLeft: Radius.circular(8.0),                      topRight: Radius.circular(8.0),                    ),                    child: Image.network(                        'https://placeimg.com/640/480/any',                        // width: 300,                        height: 150,                        fit:BoxFit.fill                    ),                  ),                  ListTile(                    title: Text('Pub 1'),                    subtitle: Text('Location 1'),                  ),                ],              ),            ),          ),        ),        Container(          margin:EdgeInsets.all(8.0),          child: Card(            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),            child: InkWell(              onTap: () => print("ciao"),              child: Column(                crossAxisAlignment: CrossAxisAlignment.stretch,                children: <Widget>[                  ClipRRect(                    borderRadius: BorderRadius.only(                      topLeft: Radius.circular(8.0),                      topRight: Radius.circular(8.0),                    ),                    child: Image.network(                        'https://placeimg.com/640/480/any',                        // width: 300,                        height: 150,                        fit:BoxFit.fill                    ),                  ),                  ListTile(                    title: Text('Pub 1'),                    subtitle: Text('Location 1'),                  ),                ],              ),            ),          ),        ),      ],    ),      ),    );  }}

output:

enter image description here


This worked for me

Image.network(imageUrl, fit: BoxFit.fitWidth,),


I don't know how.. But this really worked to keep image with fixed size in containerJust add Alignment in container

    Container(      height: double.infinity,      alignment: Alignment.center, // This is needed      child: Image.asset(        Constants.ASSETS_IMAGES + "logo.png",        fit: BoxFit.contain,        width: 300,      ),    );