In flutter, you can put widgets one above another using the flutter stack widget. For example, you can put a container above the container, text above the container, a button above the container, and many more. In this entire tutorial, you will know how to implement the flutter stack widget. You will get a complete overview of the flutter stack widget.
How to create a Flutter Stack Widget
You can create a flutter stack widget using the Stack() class. The class is called using its constructor Stack() that accepts different properties. You will know the implementation of mostly used properties here that will be very useful while designing your Mobile app user interface.
Let’s create a Flutter stack with widgets. Use the below lines of code.
Stack(
children: [
Container(
alignment: Alignment.center,
width: 500,
height: 250,
color: Colors.red,
),
Container(
padding: EdgeInsets.all(20),
margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
alignment: Alignment.center,
width: 400,
height: 200,
color: Colors.yellow,
),
Container(
padding: EdgeInsets.all(20),
margin: EdgeInsets.fromLTRB(30, 30, 30, 30),
alignment: Alignment.center,
width: 200,
height: 100,
color: Colors.teal,
),
],
),
Output

You can see the all the three containers are one above the other like a stack.
How to use Stack Widget
Now you understand how to create a Stack Widget. Let’s know how to manipulate Stack Widget. There are many properties inside the Stack() constructor class that allows you to do. These are alignment, children, fit. You will know each of them in detail.
1.children
The children properties allow you to add an array of widgets. It can be a container, row, column, stack itself, and many more. In our case, I have used three containers with different background colors.
2.alignment
The second property that allows you to manipulate Stack is alignment. It usess the Alignment enum for positioning all the children of the stack. All the enum are below.
Alignment.center


Alignment.topCenter

Alignment.topLeft

Alignment.topRight

3. fit
StackFit.expand
Stack(
alignment: Alignment.bottomRight,
fit: StackFit.expand,
children: [
Container(
width: 500,
height: 250,
color: Colors.red,
),
Container(
padding: EdgeInsets.all(20),
margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
width: 400,
height: 200,
color: Colors.yellow,
),
Container(
padding: EdgeInsets.all(20),
margin: EdgeInsets.fromLTRB(30, 30, 30, 30),
width: 200,
height: 100,
color: Colors.teal,
),
],
),
Output

StackFit.loose
The constraints passed to the stack from its parent are loosened.
For example, if the stack has constraints that force it to 350×600, then this would allow the non-positioned children of the stack to have any width from zero to 350 and any height from zero to 600. In case our example it will the same output as the above.

StackFit.passthrough
The constraints passed to the stack from its parent are passed unmodified to the non-positioned children.
For example, if a Stack is an Expanded child of a Row, the horizontal constraints will be tight and the vertical constraints will be loose.
Output

These are examples of complete implementation of the flutter stack widget. I hope you have liked this entire tutorial. If you have any queries then you can contact us for more help.
Leave a Reply