I would like to paint widgets outside the canvas and display only cropped portion ie., the screen's size or my own size and I want to do this dynamically.
Eg:sample img
I was able to do this by using
UnconstrainedBox(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.lightBlue,
//width: MediaQuery.of(context).size.width,
//height: MediaQuery.of(context).size.height ,
width: _size.width,
height: _size.height,
child: FittedBox(
alignment: Alignment.bottomCenter,
fit: BoxFit.none,
child: Container(
width: _size.width,
height: _size.height * 2,
child: MyWidget,
),
),
),
),
)
But using this setup allows me only to scale the widget to 3x since the alignment coordinates have only 3 positions along one axis. I want to be able to scale the widget to nx and display any of the portion. Pls help.
Found the solution, used Alignment(x,y) to scale and position the widget ;)
Related
How do I align one widget of a column in the center and put the rest around it?
If I had a Container or so that holds the three input fields and the button and another one that is the box above. How do I align the Container in the middle and put the box in the remaining space without moving the center container?
I tried using Expanded or Flexible but couldnt figure out how to align the widgets in that kind of way.
You can use Column like this:
Column(
children: [
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 40,
width: 40,
color: Colors.yellow,
),
)),
Container(
color: Colors.red,
height: 100,
width: double.infinity,
),
Expanded(
child: Align(
alignment: Alignment.topCenter,
child: Container(
height: 40,
width: 40,
color: Colors.green,
),
)),
],
),
I have this scenario:
i have an app that records video and this video must be square, for this reason in build method I setup camera preview in this way:
var size = MediaQuery.of(context).size.width;
return Container(
width: size,
height: size,
child: ClipRect(
child: OverflowBox(
alignment: Alignment.center,
child: FittedBox(
fit: BoxFit.cover,
child: Container(
width: size / _controller.value.previewSize.width,
height:
size / _controller.value.previewSize.height,
child: CameraPreview(this._controller), // this is my CameraPreview
),
),
),
),
);
In this way video is square but only in visualization, when I export it it is rectangular.
There is a way to set the aspect ratio of the camera in order to have a square video output?
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 ?
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.
In a Detail page, I'd like an image to take the whole width available, on the very top. This is my code:
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
child: Column(
children: <Widget>[
Container(
width: double.infinity,
child: Image.asset(
this.imageLink,
),
)
],
),
),
),
);
The problème is : with the emulator, the result is exactly what I want :
But when I launch the app on a real phone (Galaxy S8 Plus) there are some paddings (top, left and right) and the image doesn't fit the screen width.
Which means that something is not correct in the way I tried to do it with the code.
Where could be the problem ?
I think the reason being original width of image is equal or more than the width of emulator and which is less than your Samsung S8+ width, you need to use
Image.asset(
this.linkImage,
width: double.infinity,
fit: BoxFit.cover,
),
You will have use expanded widget for that or you can use
height: double.infinity,
width: double.infinity,
or
new Image.asset('assets/images/umbrella.png',
width: size.width,
height: size.height,
fit: BoxFit.fill,
)
you can use
fit: BoxFit.cover,