Equivalent of CSS's numerical vertical-align in Flutter - 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.

Related

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

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.

In Flutter, what is the difference between Padding and the SizedBox widget?

Since both are used to add spacing to our screen how do we decide when to use either of them .
SizedBox creates space between widget to widget only just height and width.
Padding is how much an element is away from itself — how much distance an element wants to keep with the elements inside it. They create distance top, bottom, left, and right.
Please checout my answer from here

Responsive padding flutter

Im trying to pass a percentage of screen size to padding widget but it says the value should be constant, also when you just type numbers it shows different UIs in different devices.
so what should we do for responsive padding.
double h = MediaQuery.of(context).size.height;
Padding(padding: const EdgeInsets.fromLTRB(h*0.2, 5.0, 50.0, 0.0),
child:...)
Remove the const keyword from your EdgeInsets. The const keyword means that Flutter knows the values that the specific widget will use beforehand. Obviously, if those values depend on the height of the device, Flutter is not able to know them until it executes your code.
Also, consider using LayoutBuilder instead of MediaQuery to get the available space constraints. In my experience, it is easier and more flexible to build the desired responsive layouts by obtaining a reference to the available screen space, instead of using the fixed device size.

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.

What does actutally Bottom overflowed by 123 pixels mean in flutter

I am learning flutter and currently switched from android to flutter.In flutter i mostly get an error something like
bottom overflowed by 234 pixels or renderbox overflowed by 340 pixels.And i fixes that problem by increase the height of the widget.If so then how to know that what much size giving to widget.I mean in android we can declare the height of the layout to be wrap content and its works perfectly.Please explain me that how can i avoid this situation because if i fixes the issue by changing the height of widget in one device then in devices of other screen sizes if throws same error ?Here is a image which throws an error.Ignore the red error , see the error in below screen.Thanks in advance.
!https://imgur.com/a/PsZzeMp
If you want to give fixed width and height to your widgets wrap it with SizedBox.
You can specify fixed width and height and you child widget will be the exact dimension.
If you want one of the dimension fixed and the other as big as the parent widget, you can try something like this:
SizedBox(
width: double.inifinity,
height: 50.0
child: Conttainer()
)
If you are worried about giving fixed dimensions. You can give the height or width according to the ratio of the screen size. You can get the height and width of the screen like this:
double width = MediaQuery.of(context).size.width;
Use the above value to give the width to your widgets like below:
Container(
width: MediaQuery.of(context).size.width * 0.8,
height: 100,
)
To answer question in the comment. You can do the following to wrap the text with dynamic content.
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 100
),
child: Text(
'ADASFASF ssss'
),
)
The above code will wrap the text to next line if the text widget is more than 100 pixels wide. As we don't have any constraints on the height.
Overflow happens when the minimum size of a child widget is bigger than the parent's constraints (width and/or height).
Which means you can:
make the parent widget bigger (eg: SizedBox(height:, width:,))
make the child smaller (eg: using a FittedBox(fit: BoxFit.scaleDown) widget)
Though the first method will not allow you a size bigger than the parent's parent widget. It should be enough to build the children relatively to their parents and not the other way.
In your case, it seems like you text + image widget is taking a little too much space. I recommend wrapping it in a FittedBox that will scale down the child widget until it fits in you bottom bar.
FittedBox(
fit: BoxFit.scaleDown,
child: _buildChildWidget(),
)
You can then wrap it in other widgets to build the layout you want (Row, Expanded, Flexible, LayoutBuilder, ...).
As the overflow issue usually happens because text or images are too big. A good exercise is trying to make your app work while setting text size to the maximum value allowed by the accessibility options of your device. You can do the same with images by setting an image size relative to your text size for example.
Paddings and margins can also cause problems because they leave less space for the child widget.