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

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.

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.

How do I move a button to a new line when it overflows?

I have several ElevatedButton that by default do not contain anything and have the same size (1 screen). Text is then passed to these buttons, which accordingly increases their size depending on the length. My problem is how do I place the ElevatedButton on the screen so that when they overflow, they are automatically transferred to the row below. (as an example, screen 2). This is true for both button states, because even if they are empty, they can also take up more space than one line and I need to move them somehow (like screen 1)
Use Wrap widget instead of Row. It will automatically wrap the buttons to next row when they can overflow
you can use Wrap:
https://api.flutter.dev/flutter/widgets/Wrap-class.html
Wrap(
spacing: 8.0, // gap between adjacent chips
runSpacing: 4.0, // gap between lines
children: <Widget>[
...
],
)

Make widget always expand to screen border

Is there a way to make a widget, for instance a Container, always expand to a screen border, not dependent of a specific widget tree structure?
Containers impose constraints on their children so you can't make a child's width bigger than its parent when it is a container
some tips :
you can use the Expanded widget to take all the remaining space
to take all the size of the screen you can use width: double.infinity
As they said already, in Container() you can use width: double.infinity or width: MediaQuery.of(context).size.width. But for this, the parent of Container can't have a size smaller than the screen.

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.