Flutter IndexedStack

Ufuk Sahin
1 min readMay 2, 2023

--

Flutter’s `IndexedStack` is a widget that displays one child from a list of children at a time, based on an index value. The widget is similar to the `Stack` widget but with a key difference: only the child at the given index is visible, while the rest of the children are invisible.

The `IndexedStack` widget is useful when you have a set of children widgets that you want to switch between, such as different screens or pages in a mobile application. By using `IndexedStack`, you can easily switch between the children widgets by changing the index value.

Here’s an example usage of `IndexedStack`:

IndexedStack(
index: _currentIndex,
children: [
FirstScreen(),
SecondScreen(),
ThirdScreen(),
],
),

In the above example, `_currentIndex` is the current index of the selected screen. Depending on the value of `_currentIndex`, the `IndexedStack` will display the corresponding child widget. For instance, if `_currentIndex` is 0, then the `FirstScreen` widget will be displayed; if it is 1, then `SecondScreen` widget will be displayed, and so on.

One benefit of using `IndexedStack` over a traditional `Stack` widget is that the `IndexedStack` widget is more efficient, since only the visible child is built and rendered, whereas in a `Stack` widget all children are built and rendered regardless of visibility.

Thanks.

https://github.com/ufukhawk

--

--