Flutter / Flutter Layouts

Understanding Stack Layouts in Flutter

In this tutorial, we'll discuss stack layouts in Flutter. You'll learn how to overlap multiple widgets over each other to create complex designs.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Learn about organizing widgets to create various types of layouts.

Understanding Stack Layouts in Flutter

1. Introduction

In this tutorial, we aim to understand the Stack widget in Flutter. Flutter is a UI toolkit from Google that lets you build natively compiled applications for mobile, web, and desktop from a single codebase. Stack is a built-in widget in Flutter which allows developers to overlap several children widgets in a simple and effective way.

What the user will learn

  • Understanding the Stack Widget
  • Types of Stack alignment
  • How to stack multiple widgets
  • Practical examples of Stack usage

Prerequisites

  • Basic knowledge of Flutter and Dart language
  • Flutter SDK and Dart installed on your machine
  • An IDE with Flutter SDK configured (VS Code, Android Studio, etc.)

2. Step-by-Step Guide

The Stack widget in Flutter allows us to put a number of widgets in the same screen space, and stack them one on top of another. This can be useful for creating complex layouts.

Understanding the Stack Widget

The Stack widget takes an array of children and orders them in a stack, one on top of another. The first widget in the array is at the bottom, and the rest of the widgets are in the order they appear in the array.

Types of Stack alignment

There are different ways you can align widgets in a Stack:
* AlignmentDirectional
* Alignment
* FractionalOffset

How to stack multiple widgets

Creating a Stack layout involves adding the Stack widget and its children to your widget tree. Here's a simple example:

Stack(
  alignment: const Alignment(0.6, 0.6),
  children: [
    CircleAvatar(
      backgroundImage: AssetImage('images/pic.jpg'),
      radius: 100.0,
    ),
    Container(
      decoration: BoxDecoration(
        color: Colors.black45,
      ),
      child: Text(
        'Flutter',
        style: TextStyle(
          fontSize: 20.0,
          fontWeight: FontWeight.bold,
          color: Colors.white,
        ),
      ),
    ),
  ],
)

3. Code Examples

Example 1: Simple Stack with Text on Image

Stack(
  alignment: Alignment.center,
  children: <Widget>[
    Image.network(
      'https://your-image-url',
      fit: BoxFit.fill,
    ),
    Text(
      'Hello Flutter',
      style: TextStyle(
        color: Colors.white,
        fontSize: 30,
        fontWeight: FontWeight.bold,
      ),
    ),
  ],
);

In this example, the Image.network widget is stacked below the Text widget, thanks to the order in which they are placed inside the children list.

Example 2: Stack with Positioned Widget

Stack(
  children: <Widget>[
    Image.network(
      'https://your-image-url',
      fit: BoxFit.fill,
    ),
    Positioned(
      bottom: 50,
      right: 50,
      child: Text(
        'Hello Flutter',
        style: TextStyle(
          color: Colors.white,
          fontSize: 30,
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
  ],
);

In this example, the Positioned widget is used to position the Text widget at a specific location in the Stack.

4. Summary

In this tutorial, we learned about the Stack widget in Flutter, how to use it to overlap multiple widgets and create complex UI designs. We also explored different Stack alignments and how to position widgets using the Positioned widget.

5. Practice Exercises

Exercise 1: Simple Stack

Create a simple Stack with an Image and Text widget, where the Text widget is positioned at the center of the Image widget.

Exercise 2: Stack with Positioned Widget

Create a Stack with an Image widget and two Text widgets. Use the Positioned widget to position one Text widget at the top left corner and the other one at the bottom right corner of the Image widget.

Exercise 3: Advanced Stack

Create a Stack with an Image widget and three Text widgets. Use the Positioned widget to position each Text widget in different corners of the Image widget.

Solutions

The solutions to these exercises can be found in the Flutter documentation and various Flutter communities online. Always remember to practice and experiment with different widgets and configurations to become proficient in using Flutter.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help