What is the difference between using Align or using Positioned flutter - layout

Her's a concrete example of using both Positionned Widget and Align Widget!
But I had a problem figuring what to use ! though original problem is to set some FABs's Offsets relatively to its Container not to the screen
Stack(
children: <Widget>[
Positioned(left: 0.0, child: Text("Top\nleft")),
Positioned(bottom: 0.0, child: Text("Bottom\nleft")),
Positioned(top: 0.0, right: 0.0, child: Text("Top\nright")),
Positioned(bottom: 0.0, right: 0.0, child: Text("Bottom\nright")),
Positioned(bottom: 0.0, right: 0.0, child: Text("Bottom\nright")),
Positioned(left: width / 2, top: height / 2, child: Text("Center")),
Positioned(top: height / 2, child: Text("Center\nleft")),
Positioned(top: height / 2, right: 0.0, child: Text("Center\nright")),
Positioned(left: width / 2, child: Text("Center\ntop")),
Positioned(left: width / 2, bottom: 0.0, child: Text("Center\nbottom")),
],
)
Example #2 (Using Align in Stack)
Stack(
children: <Widget>[
Align(alignment: Alignment.center, child: Text("Center"),),
Align(alignment: Alignment.topRight, child: Text("Top\nRight"),),
Align(alignment: Alignment.centerRight, child: Text("Center\nRight"),),
Align(alignment: Alignment.bottomRight, child: Text("Bottom\nRight"),),
Align(alignment: Alignment.topLeft, child: Text("Top\nLeft"),),
Align(alignment: Alignment.centerLeft, child: Text("Center\nLeft"),),
Align(alignment: Alignment.bottomLeft, child: Text("Bottom\nLeft"),),
Align(alignment: Alignment.topCenter, child: Text("Top\nCenter"),),
Align(alignment: Alignment.bottomCenter, child: Text("Bottom\nCenter"),),
Align(alignment: Alignment(0.0, 0.5), child: Text("Custom\nPostition", style: TextStyle(color: Colors.red, fontSize: 20.0, fontWeight: FontWeight.w800),),),
],
);
> Blockquot
e

Positioned is an offset based alignment which uses DP as unit
Align use a % of the parent size
As such, Alignment(0.1, 0.1) cannot be represented using a Positioned. And similarly,Align cannot represent a Positioned(top: 10, left: 10).
Secondly, Positioned is on a different flow.
Stack can size itself based on the size of one of its children excluding Positioned widgets.
As such, using an Align vs Positioned can result in Stack taking a different size.

Positioned can only be used within a Stack and positions a child relative to the Stack size.
https://docs.flutter.io/flutter/widgets/Positioned-class.html
Align will be as big as possible within its parent (or a size relative to the children if heightFactor, widthFactor are passed) and positions its child relative to itself. Align can be used everywhere, not only within a Stack.
https://docs.flutter.io/flutter/widgets/Align-class.html

Kind of related but on a slightly different subject,
If anyone comes here looking for a solution to why their widget doesn't precisely align where they put it inside a stack (whether it's Positioned or Align). That is probably caused by some padding value that you're not aware of it's there. Simply, open up dev tools and go through widgets one by one to check if there are any additional spaces.
And on a more rare occasion, if you wrapped one of the top widget tree elements with SafeArea, it also adds padding to both the bottom and the top. It may have been caused by that too. Just a heads up :')

Related

How do i create svg box opening animation?

It looks "broken". Transform origin of the right element is out of place. I am trying to make this box "solid", but it scatters.
http://codepen.io/HappyHarlequin/pen/bZWQro
svg:hover #right{
animation: open_right 1s linear infinite;
animation-direction: alternate;
}
svg:hover #left{
transform-origin: 0% 50%;
animation: open_left 1s linear infinite;
animation-direction: alternate;
}
#keyframes open_right{
0% {
}
100% {
transform-origin:100% 50%;
transform: rotate(230deg) rotateX(-230deg)
}
}
#keyframes open_left{
0% {
}
100% {
transform-origin: 0% 50%;
transform: rotate(-230deg) rotateX(230deg)
}
}
rotateX() is a 3D rotation and you can't do 3D transformations on elements inside an SVG. You can only apply two dimensional transforms to them (rotate, scale, translate etc in X and Y only)
Even if you could do 3D transforms, you are trying to apply a 3D transform to something that is a two dimensional shape drawn to look like it is 3D. So it would not work anyway.
Possible approaches
You could:
Stick with 2D and use keyframe animation to animate your box flaps. Draw a series of keyframes of your flaps as they open. Then step, or morph, between those shapes.
Change your flaps to be HTML elements, such as a <div>. Then position them so that they line up with your fake 3D box and have the right amount of perspective. Then you can apply a 3D rotation to those.
Switch to a proper 3D box and animate that. There are various JS libraries that can help you with that, such as three.js.

glsl layout with glvertexattribpointer issues

So I have a working program that displays a terrain mesh generated from a height map. I want to use the layout qualifier to declare the vertex attributes now, but I am having issues. When I just use the built in gl_Normal for my normals, everything works fine. When I use a normal declared with the layout qualifier, it does not work. The vertices display just fine regardless. I'm not sure as to what I'm doing wrong here.
Here is the generation of the buffers:
glGenBuffers(1, &TerrainPVBO);
glBindBuffer(GL_ARRAY_BUFFER, TerrainPVBO);
glBufferData(GL_ARRAY_BUFFER, Terrains[IID].Points.size() * sizeof(vec3f), &Terrains[IID].Points[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &TerrainNVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TerrainNVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Terrains[IID].Normals.size() * sizeof(vec3f), &Terrains[IID].Normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
Then down in my rendering function:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TerrainIVBO);
glPatchParameteri(GL_PATCH_VERTICES, 3);
glDrawElements(GL_PATCHES, Terrains[IID].Index.size(), GL_UNSIGNED_INT, 0);
If I do not use The VertexAttribArray/Pointer, and I use glVertexPointer/glNormalPointer in the drawing function (with gl_Normal/gl_Vertex in the shader), everything works fine.
This is the shader, if that helps at all:
#version 420
uniform mat3 NormalMatrix;
layout (location = 0) in vec4 vertex;
layout (location = 1) in vec3 normal;
out gl_PerVertex{
vec4 gl_Position;
};
out vec3 normalTCS;
void main()
{
gl_Position = vertex;
normalTCS = NormalMatrix * normal;
}
I'm sure I'm doing something stupid here, but I find this process a bit confusing. Any help would be greatly appreciated!
I'm not really sure what your isse is, but using GL_ELEMENT_ARRAY_BUFFER for your normals is not going to work. The glVertexAttribPointer() calls will always reference the currently bound GL_ARRAY_BUFFER, so you set up your noramls to be identical to your positions.
However, glNormalPointer() works the same way, so it is toally unclear to me how this could have worked before.

Susy maths adjustment to particular layout

Working on a Drupal site with a Zen sub theme, I ditched zen-grids for susy. I have been playing around with Susy and it works nicely. But here is my question.
I have a layout with two main columns: #content (holding the main content; a grid of three rows of four images with a width of 181px) and div.region-sidebar-second (holding the main navigation).
My designer came up with a 960px layout, where the #content container’s width is 750px then there is a 8px right-margin and div.region-sidebar-second has a width of 202px.
In my responsive.scss I put:
.sidebar-second {
#content {
#include span-columns(9, 12);
}
.region-sidebar-second {
#include span-columns(3 omega, 12);
}
}
So #content uses the first 9 columns and .region-sidebar-second uses the last 3 columns. But this does not translate to 750px/8px/202px.
The percentages that susy calculates are correct, of course, but for my layout I would need Susy to calculate different percentages.
E.g. Susy calculates for #content
.sidebar-second #content {
float: left;
margin-right: 1.40845%;
width: 74.6479%;
}
and for
.sidebar-second .region-sidebar-second {
float: right;
margin-right: 0;
width: 23.9437%;
}
In order to meet the design requirements I would need a width: 78.125% for #content, .833333% for margin-right and a width: 21.0416666% for .region-sidebar-second
Not sure whether this can be achieved with Susy span-columns. If you have a quick idea this would be very much appreciated. Thanks.

Scaling SVG objects defined in pixels to physical units?

I have an SVG element defined with a width and height of "297mm" and "210mm", and no viewbox. I do this because I want to work inside an A4 viewport, but I want to create elements in pixels.
At some point, I need to scale up an object defined in pixels so that it fits into part of the A4 page. For example, I might have an object which is 100 px wide, that I want to scale to 30mm horizontally.
Is this possible? I think I need a second container with a new co-ordinate system, but I can't find any way to do this.
EDIT
It seems that I (or SVG) has misunderstood what a pixel is. I realised that I could size a line to 100%, and then get it's pixel size with getBBox to find the scaling required. I wrote this code and ran it on 2 clients, one with a 1280x1024 monitor (80dpi), and one with a 1680x1050 LCD (90dpi):
function getDotsPerInch() {
var hgroup, vgroup, hdpi, vdpi;
hgroup = svgDocument.createElementNS(svgNS, "g");
vgroup = svgDocument.createElementNS(svgNS, "g");
svgRoot.appendChild(hgroup);
svgRoot.appendChild(vgroup);
drawLine(hgroup, 0, 100, "100%", 100, "#202020", 1, 1);
drawLine(vgroup, 100, 0, 100, "100%", "#202020", 1, 1);
hdpi = hgroup.getBBox().width * 25.4 / WIDTH;
vdpi = vgroup.getBBox().height * 25.4 / HEIGHT;
drawText(hgroup, "DPI, horizontal: " + hdpi.toFixed(2), 100, 100);
drawText(hgroup, "DPI, vertical: " + vdpi.toFixed(2), 100, 120);
}
IE9, FF, Opera, and Chrome all agree that both monitors are 96 dpi horizontally and vertically (although Opera's slightly inaccurate), and Safari reports 0 dpi on both monitors. So svg just appears to have defined "pixels" as "96dpi". some quick Googling appears to confirm this, though I haven't found anything definitive, and most hits give 90dpi, with 96dpi as the FF variant.
You can nest <svg> elements and use the viewBox attribute on the child element to get a new coordinate system. Give the child <svg> element x, y, width and height attributes of where you want it to appear on the parent in the parent's co-ordinate system.

3 sided box shadow on top of two sided box shadow

I 'm trying to create a two sided box shadow in two seperate elements, sandwiched by a three sided box shadow on the bottom and a three sided box shadow element on top. Like the image below:
I'm close, but as you can see, there is a bit of a space issue between the 3rd and 4th elements, and the top shadow is stopping in the middle for some reason...here is the CSS I have:
#tile1{
-webkit-box-shadow: 7px 0 5px #319a00 , -7px 0 5px #319a00, 0 -2px 5px 5px #319a00;
}
#tile2{
-webkit-box-shadow: 7px 0 5px #319a00 , -7px 0 5px #319a00;
}
#tile3{
-webkit-box-shadow: 7px 0 5px #319a00 , -7px 0 5px #319a00;
}
#tile4{
-webkit-box-shadow: 7px 0 5px #319a00 , -7px 0 5px #319a00, 0 3px 5px 5px #319a00;
}
Any ideas on how to make this happen? I can't just put a box shadow to the whole div because I'm going to have these element toggle.
You CAN put a box-shadow on the whole div even if you're going to toggle those inner elements. All you have to do is put a min-height property (make this equal to the height when all the boxes are toggled off) on your div instead of the height property and it will work.

Resources