How to have a container fit it's child flutter - flutter

I'm trying to replicate a feature I like in twitter.
As you can see from the images above Twitter images are always the exact same width but the height are in respect to the image. I have been able to semi replicate this idea using BoxFit.contain but the Container doesn't fit the image.
What I have implemented]
Container(
width: 290.0,
// height: 400,
constraints: const BoxConstraints(
maxHeight: 350,
minHeight: 150,
),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(27.5),
image: DecorationImage(
image: AssetImage(image[itemIndex]),
fit: BoxFit.fitWidth,
),
boxShadow: const [
BoxShadow(
color: Color(0x80000000),
offset: Offset(0, 2.5),
blurRadius: 5,
),
],
),
),
I tried a FittedBox with no luck. I attempted a FractionallySizedBox but kept on getting an error!
If anybody could lead me in the right direction I would appreciate it!

You can try fixed width on Container. But most important is using fit: BoxFit.cover,
Container(
width: 290.0,
// height: 400,
constraints: const BoxConstraints(
maxHeight: 350,
minHeight: 150,
),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(27.5),
image: DecorationImage(
image: AssetImage(image[itemIndex]),
fit: BoxFit.cover, //
),
boxShadow: const [
BoxShadow(
color: Color(0x80000000),
offset: Offset(0, 2.5),
blurRadius: 5,
),
],
),
),

Constraints go down. Sizes go up. Parent sets position.
Instead of using the image as a Container background image, use it as the Container's child property.
Container(
width: 290,
constraints: const BoxConstraints(
maxHeight: 350,
minHeight: 150,
),
child: Card(
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
margin: const EdgeInsets.all(12),
child: Image.asset(
image[itemIndex],
fit: BoxFit.cover,
),
),
);
Reference: Flutter - Understanding Constraints
Code Snippet: See result here.

This is the config that worked for me. It's a combo of the 2.
ClipRRect(
borderRadius:
BorderRadius.circular(27.5),
child: Container(
//width: 290.0,
constraints: const BoxConstraints(
maxHeight: 350,
minHeight: 150,
),
decoration: const BoxDecoration(
color: Colors.red,
/*image: DecorationImage(
image: AssetImage(image[itemIndex]),
fit: BoxFit.cover,
),*/
boxShadow: [
BoxShadow(
color: Color(0x80000000),
offset: Offset(0, 2.5),
blurRadius: 5,
),
],
),
child: Image.asset(
image[itemIndex],
width: 290,
fit: BoxFit.cover,
),
),
),

Related

Flutter Container height/width

Hey guys, so my problem is that the image should take all the space in the box (I got that) but the image looks weird.
Could you help me?
child: Container(
margin: const EdgeInsets.fromLTRB(10, 20, 10, 20),
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: Colors.grey.shade700,
),
),
height: 350,
width: 350,
child: Container(
height: 350,
width: 350,
child: FittedBox(child: infoBank[PicNumber].image,
fit: BoxFit.fill,
),
),
),
You can do this using BoxFit.cover in your Image:
fit: BoxFit.cover
Alternatively, use BoxFit.fillHeight or BoxFit.fillWidth where appropriate for only one direction.
child: Image.network(
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Peanuts_maize_chips_2.jpg/1920px-Peanuts_maize_chips_2.jpg',
fit: BoxFit.cover,
),

Boxdecoration fit parent height

I need to make a container with background image and background color in the bottom 30 pixels, but I need the layout to look like this:
SizedBox(
height: 540,
child: Container(
width: 392.7,
height: 510,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(25),
bottomRight: Radius.circular(25)),
image: DecorationImage(
image: Image.network(
'https://ak.picdn.net/shutterstock/videos/8258557/thumb/1.jpg',
).image,
fit: BoxFit.cover)
as per your ui I think u have to use stack instead of column use stack and it will provide your desire output
Here is code example:-
SizedBox(
height: 540,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image:
/// your image
fit: BoxFit.fill,
)),
),
Positioned(
bottom: 0.0,
child: Container(
height: 30,
// set color and border radius as u need
)),
],
),
);
You can simply use Column to and use another Container or Material... widget to provide extra 30px border.
SizedBox(
height: 540,
child: Column(
children: [
Container(
width: 392.7,
height: 510,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(25),
bottomRight: Radius.circular(25)),
image: DecorationImage(
image: Image.network(
'https://ak.picdn.net/shutterstock/videos/8258557/thumb/1.jpg',
).image,
fit: BoxFit.cover),
),
),
Container(
color: Colors.deepPurple,
height: 30.0,
width: 392.7,
)
],
),
)
But For background cases you can use Stack for it.
SizedBox(
height: 540,
width: 392.7,
child: Stack(
children: [
Container(
color: Colors.deepPurple,
),
Container(
width: 392.7,
height: 510,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(25),
bottomRight: Radius.circular(25)),
image: DecorationImage(
image: Image.network(
'https://ak.picdn.net/shutterstock/videos/8258557/thumb/1.jpg',
).image,
fit: BoxFit.cover),
),
),
],
),
)
More about Stack
Try this code below and let me know if change required.
SizedBox(
height: 540,
child: Column(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg",
),
fit: BoxFit.fill,
)),
)),
Container(
height: 30,
color: Colors.red,
)
],
),
)

How to add an icon with opacity layer over circular image in Flutter

I need to add camera icon with opacity layer over circular image.
I tried below code:
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(userInfo.imageUrl),
fit: BoxFit.cover,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
)
But I did not get the expected result:
Anyone have an idea how to make it works?
Thanks
Wrap it in the ClipRRect widget like this
Container(
height: 100,
width: 100,
child: ClipRRect(
borderRadius: BorderRadius.circular(50.0),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage("assets/imgs/avatar-default.png"),
fit: BoxFit.cover,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
),
),
),
You could also use ClipOval
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(userInfo.imageUrl),
fit: BoxFit.cover,
),
),
child: ClipOval(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
)
),

Flutter image does not fit in

Container(
width:
MediaQuery.of(context).size.width / 4.4,
height:
MediaQuery.of(context).size.height / 10,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black12,
width: 1.5,
style: BorderStyle.solid),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
child: (TmpBytesImage == null)
? Icon(Icons.camera_alt,
color: Colors.black26)
: Image.memory(TmpBytesImage,
fit: BoxFit.fill),
),
I expected was to fill Container with images.
but image looks like ignore BoxDecoration
How to get image to look like this Container?
Simply use Card, give it shape and put your Container inside it. Simple example:
Card(
elevation: 12,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: Container(
padding: EdgeInsets.all(20),
width: MediaQuery.of(context).size.width / 2,
height: MediaQuery.of(context).size.height / 4,
child: Image.asset(
chocolateImage,
fit: BoxFit.fill,
),
),
),
),
try to add the image inside the container decoration
like this
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage('assets/image.jpg'),
fit: BoxFit.cover,
),

How to do Rounded Corners Image in Flutter

I am using Flutter to make a list of information about movies. Now I want the cover image on the left to be a rounded corners picture. I did the following, but it didn’t work. Thanks!
getItem(var subject) {
var row = Container(
margin: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Container(
width: 100.0,
height: 150.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
color: Colors.redAccent,
),
child: Image.network(
subject['images']['large'],
height: 150.0,
width: 100.0,
),
),
],
),
);
return Card(
color: Colors.blueGrey,
child: row,
);
}
as follows
Use ClipRRect it will work perfectly.
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
subject['images']['large'],
height: 150.0,
width: 100.0,
),
)
1. Circular image (without border)
Using CircleAvatar:
CircleAvatar(
radius: 48, // Image radius
backgroundImage: NetworkImage('imageUrl'),
)
Using ClipRRect:
ClipOval(
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
)
2. Circular image (with border)
Using CircleAvatar:
CircleAvatar(
radius: 56,
backgroundColor: Colors.red,
child: Padding(
padding: const EdgeInsets.all(8), // Border radius
child: ClipOval(child: Image.network('imageUrl')),
),
)
Using ClipRRect:
Container(
padding: EdgeInsets.all(8), // Border width
decoration: BoxDecoration(color: Colors.red, shape: BoxShape.circle),
child: ClipOval(
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
),
)
3. Rounded image (without border)
ClipRRect(
borderRadius: BorderRadius.circular(20), // Image border
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
)
4. Rounded image (with border)
final borderRadius = BorderRadius.circular(20); // Image border
Container(
padding: EdgeInsets.all(8), // Border width
decoration: BoxDecoration(color: Colors.red, borderRadius: borderRadius),
child: ClipRRect(
borderRadius: borderRadius,
child: SizedBox.fromSize(
size: Size.fromRadius(48), // Image radius
child: Image.network('imageUrl', fit: BoxFit.cover),
),
),
)
There are other ways, like using DecoratedBox but that would make the answer bit too long.
You can also use CircleAvatar, which comes with flutter
CircleAvatar(
radius: 20,
backgroundImage: NetworkImage('https://via.placeholder.com/140x100')
)
Try this instead, worked for me:
Container(
width: 100.0,
height: 150.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover, image: NetworkImage('Path to your image')),
borderRadius: BorderRadius.all(Radius.circular(8.0)),
color: Colors.redAccent,
),
),
Container(
width: 48.0,
height: 48.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: NetworkImage("path to your image")
)
)),
Try this, with CircleAvatar and load image with CachedNetworkImage.
CircleAvatar(
radius: 45,
child: ClipOval(
child: CachedNetworkImage(
imageUrl: "https:// your image url path",
fit: BoxFit.cover,
width: 80,
height: 80,
),
),
),
if you want border also, then add
backgroundColor: Colors.deepOrangeAccent,
inside this
CircleAvatar(
radius: 45,
backgroundColor: Colors.deepOrangeAccent,
child: ClipOval(
child: CachedNetworkImage(
imageUrl: "https:// your image url path",
fit: BoxFit.cover,
width: 80,
height: 80,
),
),
),
For image use this
ClipOval(
child: Image.network(
'https://url to your image',
fit: BoxFit.fill,
),
);
While for Asset Image use this
ClipOval(
child: Image.asset(
'Path to your image',
fit: BoxFit.cover,
),
)
This is the code that I have used.
Container(
width: 200.0,
height: 200.0,
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage('Network_Image_Link')),
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(25.0)),
),
),
Thank you!!!
you can use ClipRRect like this :
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(25),
child: Image.asset(
'assets/images/pic13.jpeg',
fit: BoxFit.cover,
),
),
)
you can set your radius, or user for only for topLeft or bottom left like :
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25)
,bottomLeft: Radius.circular(25)),
child: Image.asset(
'assets/images/pic13.jpeg',
fit: BoxFit.cover,
),
),
)
With new version of flutter and material theme u need to use the "Padding" widgett too in order to have an image that doesn't fill its container.
For example if you want to insert a rounded image in the AppBar u must use padding or your image will always be as high as the AppBar.
Hope this will help someone
InkWell(
onTap: () {
print ('Click Profile Pic');
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ClipOval(
child: Image.asset(
'assets/images/profile1.jpg',
),
),
),
),
Use ClipRRect with set image property of fit: BoxFit.fill
ClipRRect(
borderRadius: new BorderRadius.circular(10.0),
child: Image(
fit: BoxFit.fill,
image: AssetImage('images/image.png'),
width: 100.0,
height: 100.0,
),
),
Use ClipRRect it will resolve your problem.
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
child: Image.network(
Constant.SERVER_LINK + model.userProfilePic,
fit: BoxFit.cover,
),
),
Output:
Using BoxDecoration
Container(
margin: EdgeInsets.all(8),
width: 86,
height: 86,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage('https://i.stack.imgur.com/0VpX0.png'),
fit: BoxFit.cover
),
),
),
Use this Way in this circle image is also working + you have preloader also for network image:
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: FadeInImage.assetNetwork(
placeholder:'asset/loader.gif',
image: 'Your Image Path',
),
)
Try This it works well.
Container(
height: 220.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
photoUrl,
),
),
),
);
user decoration Image for a container.
#override
Widget build(BuildContext context) {
final alucard = Container(
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
image: new DecorationImage(
image: new AssetImage("images/logo.png"),
fit: BoxFit.fill,
)
)
);
Image all side rounder corner try this one
Container(
// height and width depend on your your requirement.
height: 220.0,
width: double.infinity,
decoration: BoxDecoration(
// radius circular depend on your requirement
borderRadius: new BorderRadius.all(
Radius.circular(10),
),
image: DecorationImage(
fit: BoxFit.fill,
// image url your network image url
image: NetworkImage(
"dynamic image url",
),
),
),
);
For Circular Image in Flutter
ClipRRect(
child: Image.asset(
"assets/images/ic_cat.png",
width: 80,
height: 80,
),
borderRadius: BorderRadius.circular(50),
))
If U want only corners of image then simple change the BorderRadius.circular like below
ClipRRect(
child: Image.asset(
"assets/images/ic_cat.png",
width: 80,
height: 80,
),
borderRadius: BorderRadius.circular(20),
))