I am trying to place an image in the center of a box (container with border). The image size is set by surrounding it with a sized box, the the border or box is being created by surrounding that with a container with box decoration like this:
InkWell(
child: Container(
decoration: BoxDecoration(border: Border.all()),
height: 50,
width: 70,
child: SizedBox(
height: 10,
width: 10,
child: Image.asset('assets/store_physical.png',
fit: BoxFit.cover)),
),
),
The problem is that the image asset it ignoring the dimensions of the sized box and taking the size from the surrounding container making the image too big.
I am not sure why this is happening unless it gets it size from the top of the widget tree which doesn't seem to make sense.
Remove width and height from Container and SizedBox, instead provide it in Image.asset()
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.blue, width: 5)),
child: Image.asset(
'assets/store_physical.png',
fit: BoxFit.cover,
height: 50, // set your height
width: 70, // and width here
),
)
I also had the same problem. You can try adding the Image.asset inside another container and then change the size of that container accordingly.
InkWell(
child: Container(
decoration: BoxDecoration(border: Border.all()),
height: 50,
width: 70,
child: SizedBox(
height: 10,
width: 10,
child: Container(
height: 40.0,
width: 40.0,
child: Image.asset(
'assets/store_physical.png',
fit: BoxFit.cover
)
)
),
),
)
When the child container is smaller than the parent, the parent doesn't know where to place it, so it forces it to have the same size. If you include the parameter alignment in the parent container, it will respect the size of the child Container:
InkWell(
child: Container(
alignment: Alignment.topLeft, //Set it to your specific need
decoration: BoxDecoration(border: Border.all()),
height: 50,
width: 70,
child: SizedBox(
height: 10,
width: 10,
child: Image.asset('assets/store_physical.png',
fit: BoxFit.cover)),
),
),
Related
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
),
],
),
),
I try to put a widget offscreen and then create a slide animation to transition inside the screen. I encountered a problem with Align widget which seems to not work as documentation states (Align, Alignment).
I have a Widget (same size as the screen) and I want to position it outside of the screen (on the left side). By their documentation, an Align widget with Alignment(-3, 0) should do the trick but, when I experimented, Align acted very different based on the child size.
I created 3 examples where I have a parent container (which takes place of the screen container) and a child container which represents the widget I want to align.
In #align1 the child moved outside of the parent container but not enough, per their documentation it should be moved by the entire width of the parent widget.
The distance from -1.0 to +1.0 is the distance from one side of the rectangle to the other side of the rectangle. Therefore, 2.0 units horizontally (or vertically) is equivalent to the width (or height) of the rectangle.
In #align2 the child moved very little and in #align3 not 'moved' at all even if the x(-10 or -100) is smaller.
Column(
children: [
// #align1
Container(
margin: EdgeInsets.only(top: 50),
color: Colors.redAccent,
width: 100,
height: 100,
child: Align(
alignment: Alignment(-3, 0),
child: Container(
color: Colors.black,
width: 50,
height: 50,
),
),
),
// #align2
Container(
color: Colors.amberAccent,
width: 100,
height: 100,
child: Align(
alignment: Alignment(-3, 0),
child: Container(
color: Colors.deepPurpleAccent,
width: 90,
height: 50,
),
),
),
// #align3
Container(
color: Colors.greenAccent,
width: 100,
height: 100,
child: Align(
alignment: Alignment(-3, 0),
child: Container(
color: Colors.deepOrange,
width: 100,
height: 50,
),
),
),
],
);
What I understood wrong and how can I accomplish what I want ?
I have this ImageIcon:
Container(
color: Colors.yellow,
padding: const EdgeInsets.all(0.0),
child: ImageIcon(AssetImage("assets/images/group.png"), size: 50,))
I get this:
How can I make the icon take all the space available?
Use FittedBox for stretch icon.
Container(
height: 50,
width: 50,
child: FittedBox(
fit: BoxFit.cover,
child: ImageIcon(AssetImage("assets/icon/camera.png"))),)
I need to make an svg image size like on the photo below
But when i'm trying to give this size through SvgPicture.asset constructor or with a SizedBox it doesn't make any effect and i'm getting something like this
here is my code of that widget
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SizedBox(
height: 29,
width: 45,
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
),
),
),
I think that my Svg picture stretches to container size but i don't know what causes such a behaviour because there is SizedBox
Container doesn't know how to align the child if it is smaller than the container itself, so it expands it to fill the containers size.
The SizedBox can also be removed as SvgPicture.asset(...) now accepts size parameters.
Based on the above information, a possible solution for you will be the following, the important bit being the addition of alignment: Alignment.center:
Container(
width: 80,
height: 80,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),
),
For further information, refer to "Layout behaviour" in the Container widget documentation here.
you can use padding or margin
Container(
width: 40,
height: 40,
padding: const EdgeInsets.all(15),
child: SvgPicture.asset(
"assets/icons/outline/edit2.svg",
),
);
Instead of wrapping it with a SizedBox, use the fields height and width of the SvgPicture.
SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),
Is something wrong with this snippet? The positioned element isn't showing up but the others are. deviceHeight is defined elsewhere as the height of the device.
The first container holds the other two containers under it. The second container appears at the top correctly, but the third one (positioned) which should be under the second one doesn't appear. Welcome to hear any alternatives.
Align(
alignment: Alignment.bottomCenter,
child: Stack(children: <Widget>[
Container(
color: Color(bgDark),
height: deviceHeight / 100 * 40,
),
Container(color: Colors.red, height: deviceHeight / 18),
Positioned(
top: 50,
child: Container(
color: Colors.green, height: deviceHeight / 1))
]))
Adding a width to the Positioned Container made it visible.
Align(
alignment: Alignment.bottomCenter,
child: Stack(
children: <Widget>[
Container(
color: Colors.blue,
height: 300,
),
Container(
color: Colors.red,
height: 200,
),
Positioned(
top: 50,
child: Container(
color: Colors.green,
height: 100,
width:100,
),
),
],
),
);
I am not sure about the exact cause of the issue but it seems Positioned needed both height and width dimensions.