Exploring Stack and IndexedStack in Flutter

Exploring the Stack and IndexedStack widget and their uses

Deven Joshi
Feb 10 · 7 min read

This article is eighth in the series of articles which take a look at Flutter built-in widgets.

  1. ListView/ScrollPhysics
  2. TextFields
  3. FloatingActionButtons
  4. Hero Widget
  5. Transform Widget
  6. Draggable/DragTarget
  7. PageView

In this article, we will take a look at the Stack widget and the types of it.


A Stack widget allows us to make multiple widgets overlay each other. This not only allows brilliant custom designs but also some really cool animations.

There are two main types of Stacks:

  1. Stack
  2. IndexedStack

Let’s explore the use of Stacks.


The Stack widget allows us to put up multiple layers of widgets onto the screen.

The widget takes multiple children and orders them from bottom to top. So the first item is the bottommost and the last is the topmost.

A visual representation of the layers of the Stack

The size of the Stack is the size of the largest member in the layer. So if the bottom layer covers the complete screen then the size of the Stack is the complete screen.

Each member in the stack needs to be positioned or aligned, or else it ends up in the top left corner by default.

As an example, let’s take three containers of shrinking size:

This gives us:

An example of the Stack widget

If you notice, the containers which are smaller have a lot more area to go to and hence default to top left. To change this, you can align or position your widget using the Align or Positioned widget.

An Align widget usually takes widget to extreme positions. So for example, if we enter top-right, we need to add extra padding to keep it neat and tidy. A Positioned widget combines these two things and lets us keep one Positioned widget instead of an Align and a Padding. We will see how in a while.

We’ll alter our example to use Align and Positioned. Let’s simply wrap our containers in an Align and then a Positioned widget.

Note: Positioned has to be a child of a Stack. There cannot be another widget in between the stack and the widget.

To simply align a widget:

Stack + Align

Here we align it to the top right. Usually, this isn’t what we actually want since we need some padding to the container from the edges. We can either use padding with Align or Positioned for more fine-grained control.

Positioned has multiple types, we’ll focus on the default.

It accepts values from all four sides telling the child how far it should be away from that respective side. If no value is given, it shrinks to the lowest possible size.

Here, we give a value to the top and right. In effect, what this means is that the child will be 40.0 away from top and right and no other constraints on the other sides. Hence, it is also aligned to the top and right sides by definition.

This gives us:

Stack + Positioned

Hence, Positioned is a better widget to use in a Stack than Align + Padding but there’s no real harm in either.

There are also other types of Positioned. A few of them are:


Sets top, right, bottom, left to 0.0 by default unless overriden. Hence, it fills the screen by default since distance from all four sides is 0.0.


Creates a Positioned object from a given Rect.

Before we move on to creating something with Stack, let’s take a look at the second type.


An IndexedStack is a stack where only one element is displayed at one time by its index.

It takes children like a usual Stack but in contrast to it, only displays one child at one time. In a way, it’s not a stack and more of a way to easily switch between children when you need to.

Let’s see an example:

When we wrap the same elements with IndexedStack and give it the index 0, it shows the bottommost child.

IndexedStack with index 0

The upper two children are not shown as the index is 0.

Note: The size is still the size of the largest element.

IndexedStack also directly takes an alignment parameter and aligns all children accordingly.

Let’s make something with Stack

Example use of Stack in an app

This is a relatively straightforward example of where we can use a Stack.

An image here is overlaid by a card which is positioned at the bottom centre.

Step 1) Add a Stack

Step 2) Add a background

Here, I’m simply using a container with an image inside it

Step 3) Add a card on top with the details

Step 4) Position the card correctly

And we’re done!

The final code is:

Some of my experiments with Stack

Stacks are more or less ubiquitous in Flutter apps, but I’ve tried to use them in a few different ways (Mostly combined with the Transform widget). Here are a few projects of mine you can take a look at.

The items in the BottomNavigationBar are made using a Stack and Transform widget.

Link: https://medium.com/flutter-community/flutter-challenge-3d-bottom-navigation-bar-48952a5fd996

Each of the column of cards here is made using a Stack.

Link: https://medium.com/flutter-community/creating-solitaire-in-flutter-946c34ef053c

Here a Stack is used to make the floating video screen.

Link: https://proandroiddev.com/flutter-challenge-youtube-ec5ff36eca9b

That’s it for this article! I hope you enjoyed it, and leave a few claps if you did. Follow me for more Flutter articles and comment for any feedback you might have about this article.

Flutter Community

Articles and Stories from the Flutter Community

Thanks to Nash

Deven Joshi

Written by

Mobile Developer (Flutter + Android) | Technical Writer | Speaker | #Hack19 Winner | AAD graduate

Flutter Community

Articles and Stories from the Flutter Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade