I have a picture 640 * 400
For example if the mobile phone resolution is 1280 * 960
Widget background = new Positioned.fill(
child: Image.asset('images/back.png'),
right: 0,
left: 0
);
the picture is magnified to 640x400 -> 1280x800.
so there appears blank in the screen.
But in this case, I want to magnify to the 1536x960,
It can fill the screen without blank.
How can I make it?
Just set the fit property of the Image to BoxFit.cover:
Positioned.fill(
child: Image.asset('images/back.png', fit: BoxFit.cover),
right: 0,
left: 0
);
Related
as you can mobile screen width is maxed to width
but in web application it's not streached to max width
Please check this
github link---https://github.com/dhaval7152/demohouse.git
I am new in flutter
Try this:
Container(
// this is the width of the screen
width: MediaQuery.of(context).size.width,
child: Scaffold(...);
),
i m a beginner to flutter and I was trying to Implement some image inside a Container in my app.
Here is my code :
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Container(
width: 200,
height: 200,
child: FittedBox(
child: Image.asset(
'assets/images/' + i.toString() + ".jpg"),
fit: BoxFit.fill,
),
),
),
My image showed up and it did filled the Container , but it looks horrible . It is super stretched , I tried changing the fit: BoxFit to something like fit : BoxFit.contain but it won't work.
Here is how my UI Looks :
The picture (Display properly but just stretched)
How do I make this image fit without stretching it.
Any answer would be appreciated , and thanks for helping.
When we use Boxfit.fill, the image will take up the complete space of parent container. Aspect ratio here would be secondary. That is why the image looks stretched.
Try Boxfit.cover. Another option would be to equal Container width to double.infinity and adjust the height, as needed.
BoxFit.fill will stretch the image to fill the space.
You should use BoxFit.cover to fill the space without stretching the image.
If you want to make sure the full width of the image is visible, use BoxFit.fitWidth.
If you want to make sure the full height of the image is visible, use BoxFit.fitHeight.
Basically I need to create some kind of Snackbar widget that can be created on any page. So I need to make it positioned relative to the screen. The only idea is to create the main Stack widget which will wrap all other pages. Do you have any other ideas? I found pretty similar question without any interesting answers
By using Mediaqueryto retrieve the screen size.
For example we can the screen width like this :
MediaQueryData screenSize = MediaQuery.of(context).size.width;
let's say i want my container's size to take up half the screen's size
One way to go about it is like this :
Container(
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width/2,
child: Text('This container is half this device screen's size');
);
What you are going for here is using the Positioned widget
Let's take the last example and leave a quarter of the screen on every side :
Container(
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width/2,
child: child: Stack(
children: <Widget>[
Positioned(
left: MediaQuery.of(context).size.width/4,
top: MediaQuery.of(context).size.height/4,
right: MediaQuery.of(context).size.width/4,
bottom: MediaQuery.of(context).size.height/4,
child: Text('This container is half this device screen's size'),
),
],
),
I have a layout where I want to expand a widget (container) to the right, it's the red area in the examples. To the left of the expanded container there is a column with an image at the topleft (blue in examples) and a button in the bottom of the column. I do know the button width, it's a bit over 100 pixels but we can assume that it's 100 pixels if that helps.
The thing is that the blue area (it's a user uploaded image) can vary in size. From e.g. 100x100 to 800x800. It will be square and smaller than 100x100 is not supported. Large images are resized to 800x800.
I want to achieve this:
The column should adapt it's size depending on the image size.
The column should be as wide as needed by the button but otherwise 100 <= width <= 400 depending on image size.
The column should not overflow in any direction.
The red area should be maximized.
The button should always be at the bottom left of the layout.
I cannot know the column height in advance without using a LayoutBuilder and I want to know if this is achievable without calculating the exact height in pixels using a LayoutBuilder.
Here's an example of how I want it too look with a small image (100x100) and that works with the code below the image:
Code that works well for 100x100 image:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Container(
height: 400,
width: 800,
child: Row(
children: <Widget>[
_buildColumn(),
Expanded(child: Container(color: Colors.red)),
],
),
),
),
);
}
Widget _buildColumn() {
return Container(
constraints: BoxConstraints(maxWidth: 400),
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_buildImage(),
MaterialButton(
color: Colors.green,
child: Text('Fixed height button'),
onPressed: () {},
)
],
),
),
);
}
Widget _buildImage() {
// This can vary from 100x100 to 800x800 (always square)
var size = 100.0;
return Container(color: Colors.blue, width: size, height: size);
}
If I bump it to an 800x800 image by changing the size variable to 800.0 in _buildImage() I get this layout:
I understand that the column have unconstrained height on it's children so it will render the blue container with 800px height which creates the overflow. I do know that the column height is 400px in this case and that the button is 48px high so I can calculate that the max image size is 352px in height. By wrapping the blue container in a Container with constraints (maxWidth: 352, maxHeight: 352) I achieve the layout that I want:
Widget _buildImage() {
// This can vary from 100x100 to 800x800 (always square)
var size = 800.0;
return Container(
constraints: BoxConstraints(maxWidth: 352, maxHeight: 352),
child: Container(color: Colors.blue, width: size, height: size),
);
}
I can also achieve this using expanded, like this:
Widget _buildImage() {
// This can vary from 100x100 to 800x800 (always square)
var size = 800.0;
return Expanded(
child: Container(color: Colors.blue, width: size, height: size),
);
}
I want to avoid using calculated pixels for height/width so lets continue with the Expanded widget. When I have that expanded widget with a small image, i.e. 100x100 I get this result (which is not what I want):
I need to align my blue square within the expanded widget to prevent it to getting stretched but when I do that (align top left), like this:
Widget _buildImage() {
// This can vary from 100x100 to 800x800 (always square)
var size = 100.0;
return Expanded(
child: Container(
alignment: Alignment.topLeft,
child: Container(color: Colors.blue, width: size, height: size),
),
);
}
Then I get this result:
The container outside of the blue container expands and makes the column unnecessary wide. I want to have the same look as the first image when I have a small image.
How can I achieve this adaptive layout without using a LayoutBuilder and calculating exact image constraints?
Wrap the column with a container and set the height:
MediaQuery.of(context).size.height * 0.8;
it will contain 80% height in screen
I have an image with a rectangle drown in it, I know the position and the size of the rectangle. I could have different images with different and multiple elements like this.
I want to position a Widget, in a way that perfecly overlaps the rectangle in the image background, this means that I need a way to map the position and scale of my rectangle to the flutter coordinates of my containing widget.
I came up with a Stack with the actual Image on the bottom, with a BoxFit.cover fit, and a Positioned with my Container inside on top of it.
It should work with every aspect ratio and pixel density.
I need some formula to fill those question marks.
Thank you.
class ImageWidget extends StatelessWidget {
static const Offset imageTargetStart = Offset(302, 281);
static const Size imageTargetSize = Size(441, 57);
#override
Widget build(BuildContext context) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
Image.asset(
'assets/sample_image.png',
fit: BoxFit.cover,
),
Positioned(
// left: ??,
// top: ??,
child: Container(
// width: ??,
// height: ??,
decoration: BoxDecoration(border: Border.all(color: Colors.red), color: Colors.greenAccent),
),
)
],
);
}
}