How to create a circular image without distortion in flutter - 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,
)

Related

Taking a picture and displaying it effect in flutter

goal: display an image picked inside a circle.
issue: the image picked is clipped on the sides.
Here is the one that I am using to display the image:
CircleAvatar(
radius: 50.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(100),
child: Image.file(_photo!,
width: 100, height: 100, fit: BoxFit.fitHeight),
),
)
the code above is used to display the image after I take a picture from the phone
this is what it looks like
The image should look like this ::
this is the code snip for when the image is being displayed correctly:
Widget _buildProfileImage() => CachedNetworkImage(
imageUrl: _cloudUser.userImage,
imageBuilder: (context, imageProvider) => CircleAvatar(
backgroundColor: Colors.grey.shade800,
radius: 50,
backgroundImage: NetworkImage(_cloudUser.userImage),
),
);
the code above is used to display the image that was stored on firebase
I am pretty sure that the issue is with the widget 'ClipRRect' but I dont know what other widget I can use. or maybe thats not the issue at all idk.
use FittedBox widget
Change this:
CircleAvatar(
radius: 50.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(100),
child: Image.file(_photo!,
width: 100, height: 100, fit: BoxFit.fitHeight),
),
)
to this code below:
ClipRRect(
borderRadius: BorderRadius.circular(100),
child: FittedBox(
alignment: Alignment.center,
fit: BoxFit.cover,
child: Image.file(_photo!)),
),
),

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 can I control image size in Flutter's CircleAvatar?

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
),

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,
),
),