How can I control image size in Flutter's CircleAvatar? - flutter

I have this Dart code in a Flutter project;
CircleAvatar(
radius: 130.0,
backgroundImage: AssetImage('assets/image.jpg'),
backgroundColor: Colors.transparent,
)
The radius parameter seem to control the size of part of the image seen through the circle 'window', this obscures part of the image because the image size is still the same. The image is 567 * 572 pixel. How can I control the size of the image as well?

Instead of using CircleAvatar, use Container and make it circular, like this:
Container(
width: 130,
height: 130,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage('assets/image.jpg'),
fit: BoxFit.fill
),
),
)
The output (ignore the blur in the background):

Try this:
Image.asset(
'assets/images.jpg',
width: 300,
height: 150,
fit:BoxFit.fill
),

Related

In Flutter, How can I make my FadeInImage fit to the parent container with rounded edges?

Here is my code. This produces a rectangle image.
return Container(
width: 325,
height: 245,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
),
child: FadeInImage.assetNetwork(
placeholder: AppAssets.eventPlaceholder,
image: tempPhoto,
fit: BoxFit.cover,
),
);
I expect the photo's corners to be cut because the parent has rounded corners.
You can wrap the widget with a ClipRRect to get rounded corners:
A widget that clips its child using a rounded rectangle.
In your case:
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
width: 325,
height: 250,
color: AppColors.white,
child: FadeInImage.assetNetwork(
placeholder: AppAssets.eventPlaceholder,
image: tempPhoto,
fit: BoxFit.cover,
),
),
);
Here is a YouTube video by the Google team explaining ClipRRect.

Flutter. Image rounded borders not working

I'm trying to make rounded corners of image. Here is my code:
ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
))
Everything works well, but when I try to fit the image into a container with a fixed height and width, the rounded borders stop working.
Here is my code:
LimitedBox(
maxWidth: 95,
maxHeight: 95,
child: ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
),
),
)
Why is this happening, please help me.
Let's try with background image
Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
image: DecorationImage(
image: AssetImage(
'assets/images/test.jpg'),
fit: BoxFit.fill,
),
),
)
output:

Images inside Container not fitting completely

I have a Container with a Row inside.
Container(
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
//mbx
Container(
width: 40.0,
height: 40.0,
decoration: new BoxDecoration(
color: AppColors.grisMovMap,
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
'assets/sports/MBX.png')))),
SizedBox(
width: 5,
),
//motocross
Container(
width: 40.0,
height: 40.0,
decoration: new BoxDecoration(
color: AppColors.grisMovMap,
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
'assets/sports/motocross.png')))),
],
),
)
The Row contains two other Containers with a circular asset image inside.
The issue is that both image are not filling the circle space, they are cut on some parts of the border.
I would like to put both images completely inside the circles, just as adding a padding value.
EDIT:
Here you have both full images to compare with the app output:
CircleAvatar(
radius: 100,
child: Padding(
padding: EdgeInsets.all(10),
child: Image.asset(
"assets/images/w3lk3.png",
),
),
backgroundColor: Colors.purple,
),
Output:
Use fit:BoxFit.contain to fit the image fully inside the container.
Since you are using circle image I would suggest using the native flutter solution for circle images as found here in the official documentation. https://api.flutter.dev/flutter/material/CircleAvatar-class.html
In terms of code:
CircleAvatar(
backgroundImage: NetworkImage(userAvatarUrl),
)
If I understood correctly you want a certain color around the circle?
Use double CircleAvatar widget. It may not be the correct way but it accomplishes the task.
CircleAvatar(
radius: 70,
backgroundColor: Colors.red,
child: CircleAvatar(
backgroundImage:
AssetImage('assets/sports/motocross.png'),
radius: 50,
),
),
Since it creates two circles just make sure the first one is bigger than the second one and set the color of your choice.

How to set Image fit on every devices in flutter

I am a beginner in Flutter and I just making a simple image carousel application using carosel_slider package. What I want is an image should look on all devices. Image width should be greater than height. The emulator shows fine, but not on my device. I have read this post, but still, I don't know how to do it.
AspectRatio(
aspectRatio: 16 / 9,
child: Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*ZMOh7JwiTAVVHF8e7sijNg.png"),
fit: BoxFit.cover,
),
),
),
),
Thanks to **
MediaQuery.of (context).size
**, you can get the size of the phone. The event you want to describe in the medium article is proportional by taking the page size instead of giving it a fixed height. this is the most logical thing anyway. But in the case of the image sometimes it may not eat. So you may need to use extra fix atribute.
Container(
alignment: Alignment.center,
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width * 0.6,
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*ZMOh7JwiTAVVHF8e7sijNg.png"),
fit: BoxFit.cover,
),
),

How to create a circular image without distortion in flutter

I need to create a circular image but when it is displayed in the container I use the photo is deformed and it shows very badly
Use this code
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent),
borderRadius: BorderRadius.circular(50),
image: DecorationImage(
image: FileImage(photoImage),
fit: BoxFit.fill,
),
),
)
I also provided this code
CircleAvatar(
radius: 40,
child: ClipOval(
child: Image(
height: 100,
fit: BoxFit.fill,
image: FileImage(photoImage),
),
),
)
I appreciate your help with this.
By very badly, I assume you're referring to the distorted aspect ratio of the image. You might want to use BoxFit.cover. BoxFit.fill is known to distort the image's aspect ratio as per the documentation
BoxFit.cover ,on the other hand maintains the aspect ratio while being small as possible.
I suggest use Circle avatar it is better
For asset images
CircleAvatar(backgroundImage: FileImage(photoImage), radius: 55.0)
CircleAvatar With network image
CircleAvatar(
backgroundImage:
NetworkImage('https://www.woolha.com/media/2020/03/eevee.png'),
minRadius: 50,
maxRadius: 75,
)