cover-img

Streamlining UI Switching with IndexedStack Widget in Flutter

Seamlessly Manage Multiple Views for a Clean User Experience

30 June, 2023

2

2

0

Introduction

In the realm of Flutter app development, efficiently managing multiple views or screens can be a complex task. Fortunately, Flutter provides a convenient solution: the IndexedStack widget. In this blog post, we will delve into the capabilities of the IndexedStack widget, explore its usage, and provide a practical example to demonstrate how it can simplify UI switching in your Flutter applications.

Understanding IndexedStack

The IndexedStack widget in Flutter allows you to display only one child at a time from a list of children based on an index value. It helps manage multiple views or screens efficiently, ensuring that only the relevant content is rendered while preserving the state of each child widget. IndexedStack is particularly useful when you want to switch between different views without incurring the cost of rebuilding each widget.

Example Usage

Let's consider a scenario where you have a screen with two different views, and you want to switch between them based on user interaction. Here's an example code snippet that demonstrates the usage of the IndexedStack widget:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'IndexedStack Widget Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const ColorFilteredWidgetDemo(),
    );
  }
}

class ColorFilteredWidgetDemo extends StatefulWidget {
  const ColorFilteredWidgetDemo({super.key});

  @override
  State<ColorFilteredWidgetDemo> createState() =>
      _ColorFilteredWidgetDemoState();
}

class _ColorFilteredWidgetDemoState extends State<ColorFilteredWidgetDemo> {
  int currentIndex = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('IndexedStack Widget Demo'),
        centerTitle: true,
      ),
      body: Center(
        child: IndexedStack(
          index: currentIndex,
          children: [
            Container(
              color: Colors.blue,
              child: const Text(
                'View 1',
                style: TextStyle(fontSize: 35),
              ),
            ),
            Container(
              color: Colors.green,
              child: const Text(
                'View 2',
                style: TextStyle(fontSize: 35),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

In the code above, we define an integer variable currentIndex to keep track of the currently selected view. Within the IndexedStack widget, we set the index property to currentIndex. The children property consists of two Container widgets representing View 1 and View 2, respectively. The currently selected view is rendered while maintaining the state of both views.

By updating the value of currentIndex based on user interaction or any other triggering mechanism, Flutter automatically switches between the views within the IndexedStack. This allows you to seamlessly navigate between different screens or sections within your app.

Output

Here's the output of the above code 👇

image.png

Conclusion

The IndexedStack widget in Flutter streamlines UI switching and enhances the user experience by efficiently managing multiple views or screens. By leveraging IndexedStack, you can ensure that only the relevant content is rendered while maintaining the state of each child widget. This results in a clean and efficient user interface, improving performance and minimizing unnecessary widget rebuilds.

For more queries regarding Fluttter and DevOps, Feel free to reach me on my Twitter and Showwcase handle 😃

Til then, Happy Coding 😄

indexedstackwidget

flutteruiswitching

efficientuimanagement

2

2

0

indexedstackwidget

flutteruiswitching

efficientuimanagement

Hasnain Makada
Elite @Showwcase | Prev: CCO @ShowwcaseHQ | Building out OSWH 👨‍💻 | MLSA - β | DevOps and Flutter 💙 | Blogger at Hashnode and Showwcase 📑

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.