The Stack widget allows child widgets to overlap on top of each other. The last child of the Stack stays on top.
Sometimes basic widgets such as Column and Row are not sufficient for creating a complex UI. For example, if want to show text on top of an image - you should consider using the Stack widget.
The following figure helps you understand how the Stack widget is different from other widgets.
Here's another example that shows the use of the Stack widget:
To add the Stack widget in your project:
- First, drag the Stack widget from the Layout Elements tab (in the Widget Panel) or add it directly from the Widget Tree tab.
- Now you can add a few CircleImage inside the Stack.
- Select any CircleImage and drag to change the alignment.
Here are the steps to create a UI as shown in figure 2 above using the Stack widget:
- Drag the Stack widget from the Layout Elements tab (in the Widget Panel) or add it directly from the Widget Tree tab.
- Add Image widget and set its width and height to inf.
- Add the Text widget and drag it to the bottom left of the Image.
You can change the way children are placed inside the Stack widget by changing the position of a specific child widget or changing the default placement.
You may want to change the position of a specific child widget to match a design specification.
Please follow these steps to change the position of a child widget:
- Select a child widget and drag it to change the alignment.
- To make a child appear outside the boundary of the Stack, keep dragging the widget and place it on the boundary of the Stack.
By default, all the children are placed at the top left corner of the stack. While creating a complex UI, you may want the children to be aligned to a specific position.
To change the default placement of the children:
- Select the Stack from the widget tree or from the canvas area.
- Go to the Property Editor and navigate to the Stack Properties section.
- Spot the Default Child Horizontal Alignment and Default Child Vertical Alignment. You can adjust this by using the slider or entering a value.
- For Default Child Horizontal Alignment, A value of -1 will set the default placement to the left, while a value of 1 will set the default placement to the right.
- For Default Child Vertical Alignment, A value of -1 will set the default placement to the top, while a value of 1 will set the default placement to the bottom.