How do I fit my image into the container? - flutter

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

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

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

Matching Width of Positioned Container to Container in Stack

I'm having issues getting the positioned container to match the width of the first container. Essentially, I want the original container to be as wide as possible hence the width is set to double.infinity. The positioned container is meant to be an overlay to help with the visibility of the text that is stacked on top of the original container. However, the issue here is if I set the width of the overlay container to double.infinity I get an error saying BoxConstraints forces an infinite width. So right now I set the width to the size of the screen...but we can see that the border radius isn't drawn because it's too large.
How do I get the overlay container to match the original container containing the image?
Stack activityDetails(BuildContext context) {
var smallRadius;
return Stack(
children: <Widget>[
ClipRRect(
borderRadius: smallRadius,
child: Container(
width: double.infinity,
height: 200,
child: CachedNetworkImage(
imageUrl: image,
fit: BoxFit.cover,
),
),
),
Positioned(
top: 0,
left: 0,
child: Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
bottomLeft: Radius.circular(0),
bottomRight: Radius.circular(0),
),
),
child: Text(
category.toUpperCase(),
),
),
),
],
);
}
If the sole goal is to only have the above display then I would suggest you replace the contents of your Positioned with this code and remove the entire ClipRRect chunk
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
image: const DecorationImage(
image:NetworkImage('src'),
fit:BoxFit.cover,
),
),
width: double.infinity,
height: 200,
child: Row(
crossAxisAlignment:CrossAxisAlignment.start,
children:const[
Text('Events'),
],
),
),
Remember to put it inside the Positioned() and edit the necessary parts like Cached and the Text() removing the const flag
However, if you still want to stick to this approach then I would suggest you try moving the Positioned top down by a few pixels

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: