Position a widget, inside a stack, but according to the center of the widget - flutter

I want to position a widget, CustomWidget, but at the specified position, i.e, top, left values, in the Positioned widget, I want there to be the center of my CustomWidget, instead of the top-left corner of my CustomWidget.
I want something like this:-
Stack(
children: [
Positioned(
left: x,
top: y,
alignment: Alignment.center, //Alignment of the child widget, such that at (x, y) there is the center of the widget.
child: CustomWidget(),
),
],
);
Here is the type of UI I am trying to make, I am trying to make both cases:-
CustomWidget does not have any specific type, I am currently trying to make a general method, It might be a simple Text Widget with some variable value, or maybe a Widget with a Row of Image, Divider and Text.
I am basically using CustomWidget as a kind of popup, which will be used in case of some extra information, and also as a stamp over a widget, wherever the user clicks, in both cases, I cannot be certain of the size.
Note:- I do want to position the CustomWidget at a specified offset in the stack, but want that there should be the center of the CustomWidget instead of the top-left corner of CustomWidget.

You don't need to use the Positioned widget. Try using the Align widget, remember to add alignment: Alignment.center, to center it.

I have developed a package, with many helpful components (basically the components, which I use often, in all my projects, so to prevent writing the same thing everywhere 😜). So there I have added this new Widget named PositionedAlign this work according to the above question. I hope this helps anyone who might need it.

Related

How to move the blue box from the right outer side of the parent component to the left outer side

How to move the blue box from the right outer side of the parent component to the left outer side,Blue box width is not fixed。
The order widget appear on the screen is dictate by the order they are listed in the Row or other widget they are wrapped in. Widgets at the top will be on the left and widgets on the bottom will be on the right.
Row(
children: [
Container(color: Colors.red),//left widget
Container(color: Colors.blue),//right widget
],
),
This is a very simplified explanation as your application grows obviously things become more complex but this principle remains true.

How to properly manage parent:child relationships

Card, by default, assumes the size of its child. So, if we want to change the size of that card, then we also need to change the size of its child.
Text, on the other hand, is a widget. Which, by default, only takes as much space as this text needs. Therefore, if you want to change the size of Text, you need to also change the size of its parent. Since the Card depends on the child and the Text depends on the parent.
Card by default does not have any height or width. The width/height depends on the Child or the Card. In the screenshot, the child here is Text() Widget. The Text widget takes as much space as it's font size. So more the font size is, the larger will be Card.
The last part of the explanation is incorrect. You can't change parent size here as "Card" widget does not has any size properties. So if u want bigger card and small text, use "Container" Widget instead of Card. Almost all developers use Container only to create Card. Container has more feature then a card has.
Container(
height: 100,
width: 100,
child: Material(
elevation: 5,
child: Text("Chart!"),
),
)

Equivalent of CSS's numerical vertical-align in Flutter

In CSS, the vertical-align property can be used to vertically align an inline element's box inside its containing line box; see
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align. It can take various fixed values, such as top, baseline, middle, which seem to have Flutter equivalents, but it can also take a numerical length, such as 10px, which I have not been able to find an equivalent of in Flutter. So, the question:
Is there a way to take a widget and vertically offset it by a specified number of logical pixels from its normal alignment according to baseline? (Say with TextSpan, or WidgetSpan, for a concrete example.)
I don't have access to the child element's height a priori, so using Align doesn't seem feasible, since that does the alignment with relative lengths.
Involving your widget in a Padding widget might work:
Padding(
padding: EdgeInsets.all(10.0),
child: YourWidget(),
)
You can specify only some sides of widget for into the Padding constructor with the padding property:
(See the EdgeInsets Class)
padding: EdgeInsets.only(bottom:10.0)
This way there will be a padding only at the bottom of the referenced widget.

Why is the flutter MediaQuery starting from top of the screen

When I am using the MediaQuery to resize a container, like this =>
height: MediaQuery.of(context).size.height - 100,
the subtraction or the resizing of the container starts from the bottom instead of from the top. Please someone should help me out
You could wrap it with Align. It happens because naturally Flutter draw the Widgets from Top Left.
Something like this:
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: MediaQuery.of(context).size.height - 100,
color: Colors.white,
),
),
As an addition to the answer by #FederickJonathan,
Align gives you some default positions to align your widget as described here: https://api.flutter.dev/flutter/painting/Alignment-class.html#constants
But you can also customize the position.
However you like, by giving Alignment as Alignment(X, Y).
X = -1.0 - 1.0, starting from left->right
Y = -1.0 - 1.0, starting from top->bottom
Fun fact:
Not Just flutter but almost all Rendering framework which paints the screen to display your views always starts from the Top-Left corner of the screen.
It is known as the origin point of the view/screen.
In flutter, if no co-ordinates or alignment is provided, then you'll see that the view is plotted starting from the top left corner.
The Origin point is also considered as the source of Light in material design. So all the shadows are cast to the bottom to the right side initially.
Update: Material Design combines various light sources for shadow. So the above fact is not true for material design but still you can see it in some other design guide lines.

Flutter - How to remove margin under GridView?

I get a strange margin under my GridView:
This image is from an IOS simulator, here's how it looks on a smaller screen on Android where the margin appears to be gone or a lot smaller:
Here's the code:
Column(
children: [
GridView.count(
shrinkWrap: true,
crossAxisCount: 8,
children: tiles
),
Text('mamma')
]
)
Each element in the grid (tiles) is an EmptyTile widget:
class EmptyTile extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: bgColor,
border: Border.all(color: borderColor)
)
);
}
}
I really can't figure out what this margin is or where it comes from, whether it has something to do with shrinkWrap or something else.
How can I remove this margin?
EDIT:
As requested here's the fullscreen images without the simplified example.
IOS:
Android:
Try this. by default it has padding and you should set padding to zero.
GridView.builder(
padding: EdgeInsets.all(0),
The difference in layout you experience comes from a combination of things:
How GridView manages its size.
How widgets are placed in a Column.
GridView tiles and size
When you build a GridView, its width and height are implicitly deduced from the crossAxisCount parameter and the constraints given by its parent.
The constraints are the limits in size in which the widget is allowed to draw.
The crossAxisCount defines how many tiles should fit in one line (row or column). Depending on its scrollDirection, it will either try to fill all the available width or height. By default this direction is set to Axis.vertical, which means that the width will be filled.
So when we come back to your example, this means that the size of each tile will depend on the width of the Column containing your grid, divided by the number of tiles you set in crossAxisCount. This size will be both the width and the height of every tile in your GridView.
Once Flutter knows the size of each of your tiles, it sets them on each row, until all tiles are placed or there isn't any available space.
Column layout
Now that we know more about GridView, we need to understand how Column builds its layout.
Columns allocates space to each widget in its children, following an algorithm best describes in the docs.
tl;dr:
Your GridView will only fill its own height in the Column, leaving the rest as free space. This is why you get empty space in your Column.
Possible fix
I actually don't really see how this is a problem. GridViews are supposed to only extend to display their children, so it totally makes sense for it to stop when it completed.
The thing is, most of the times this ind of grids are not used with a finite list of children, and more likely with a growing list.
If you only want to have one line of tiles, that will extend using the available space, you could use a simple Row.
If you want multiple lines of tiles, with non-square tiles, you need to dive a little deeper into GridView.custom.
Edit after question was updated with more screenshots:
It is possible that you need to rethink your layout so that the player panels are in the same Column than the game board. You will have a much better control over the layout this way.