Taking a picture and displaying it effect in flutter - 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!)),
),
),

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 - How to adjust position of an image in a container with fit:boxfit.cover

so i have a container that displays an image, and the image fitted to the container with fit:BoxFit.cover, here is the code:
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
"assets/images/MojitoImage.png"),
),
),
),
the result of the code will looks like this
i just want to change the position of the image down a little bit to the bottom, so it can be looks like this (there is a little black space above the leaf)
any solution how to do that?
Try alignment: Alignment.topCenter
You can simply position the image with Stack and Position widget like this:
Container(
color: Colors.grey,
width: 80,
height: 80,
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
Positioned(
bottom: 10.0,
right: 10,
left: 10,
child: Icon(Icons.receipt,
size: 50.0, color: Colors.greenAccent[400]), //Icon
),
],
),
),

How to fit an image in a circle button properly in flutter?

The images I have been adding are not fitting properly in the circular shape.
This is the image for reference
And this is the code for reference
Container(
child:Material(
shape: CircleBorder(),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: InkWell(
splashColor: Colors.black26,
onTap: (){},
child: Ink.image(
image: AssetImage('assets/'+ Name),
height: 60 ,
width: 60,
),
),
)
),Text(String),
Use fit property of Ink.child
1st way : Use fit: BoxFit.cover, for center cropped image
Or else
2nd way : Use fit: BoxFit.fill, to stretch the image
Container(
child:Material(
shape: CircleBorder(),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: InkWell(
splashColor: Colors.black26,
onTap: (){},
child: Ink.image(
image: AssetImage('assets/'+ Name),
fit: BoxFit.cover, //Add this line for center crop or use 2nd way
height: 60 ,
width: 60,
),
),
)
),Text(String),
I guess you can use
CircleAvatar
here is the demo code
CircleAvatar(
radius: 20.0,
child: ClipOval(
child: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0sCAvrW1yFi0UYMgTZb113I0SwtW0dpby8Q&usqp=CAU')),
),
though I can't open your example image, I guess if you use Circle Avatar it will work

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 do I fit my image into the container?

I would like to get rid of the whitespace. The result currently is as below:
The white image is taking extra space and doesn't fit into the container.
Below is my code:
Container(
height: 180,
width: 160,
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.network(book.pic == null ?
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Book-icon-bible.png/900px-Book-icon-bible.png'
:book.pic),
),
),
You can use fit: BoxFit.cover. Also you don't need to use ClipRRect in this case, you can just use the container's own property to make the edges smoothed out like the example bellow.
Container(
height: 180,
width: 160,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
),
child: Image.network(
book.pic == null
? 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Book-icon-bible.png/900px-Book-icon-bible.png'
: book.pic,
fit: BoxFit.cover,
),
),