image

11 May 2026

9K

35K

Flutter Layout Tips: Harnessing Align, Stack, Positioned, and LayoutBuilder for Creative UI

Flutter's declarative UI model makes building beautiful user interfaces a joy. While basic layouts often rely on widgets like Column, Row, and Container, creating truly unique and responsive designs requires a deeper understanding of more specialized layout widgets. This article explores four powerful widgets—Align, Stack, Positioned, and LayoutBuilder—and demonstrates how to leverage them for creative and dynamic UI.

1. Align: Precision Positioning Within a Parent

The Align widget is used to position its single child within itself according to an AlignmentGeometry. It ensures that its child occupies a specific spot relative to its own bounds. This is incredibly useful when you want to place a widget at a specific corner, center, or any intermediate point within an available space.

Key Property:

  • alignment: A property that takes an Alignment object (e.g., Alignment.topLeft, Alignment.center, Alignment(0.5, 0.5)).

Example:

Here, we use Align to place text at the bottom-right corner of a Container.


import 'package:flutter/material.dart';

class AlignExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Align Example')),
      body: Center(
        child: Container(
          width: 200,
          height: 200,
          color: Colors.blueGrey[100],
          child: Align(
            alignment: Alignment.bottomRight,
            child: Text(
              'Bottom Right',
              style: TextStyle(fontSize: 16, color: Colors.blueAccent),
            ),
          ),
        ),
      ),
    );
  }
}

2. Stack: Layering Widgets on Top of Each Other

Stack is a widget that allows you to overlay multiple children widgets on top of each other, similar to layers in a design tool. Children can be positioned relative to the Stack's edges. This is fundamental for creating complex UIs like an image with overlaid text, badges, or custom controls.

Key Properties:

  • alignment: How unpositioned children are aligned within the stack (defaults to AlignmentDirectional.topStart).
  • fit: How unpositioned children in the stack are sized (e.g., StackFit.expand to fill the stack).

Example:

This example demonstrates layering an image with a text label at the bottom.


import 'package:flutter/material.dart';

class StackExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Stack Example')),
      body: Center(
        child: Container(
          width: 300,
          height: 200,
          color: Colors.grey[200],
          child: Stack(
            children: [
              Image.network(
                'https://picsum.photos/300/200',
                fit: BoxFit.cover,
                width: 300,
                height: 200,
              ),
              Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  color: Colors.black.withOpacity(0.5),
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'Beautiful Scenery',
                    style: TextStyle(color: Colors.white, fontSize: 18),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

3. Positioned: Absolute Positioning Within a Stack

While Align can be used within a Stack, the Positioned widget is specifically designed to control the precise positioning of children *within* a Stack. It allows you to specify exact distances from the stack's edges (top, bottom, left, right) or specific width and height for its child.

Key Properties:

  • top, bottom, left, right: Distances from the corresponding edge of the Stack.
  • width, height: Explicit dimensions for the child.

Example:

Building on the Stack example, we'll use Positioned to place a small badge at the top-right corner of the image.


import 'package:flutter/material.dart';

class PositionedExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Positioned Example')),
      body: Center(
        child: Container(
          width: 300,
          height: 200,
          color: Colors.grey[200],
          child: Stack(
            children: [
              Image.network(
                'https://picsum.photos/300/200',
                fit: BoxFit.cover,
                width: 300,
                height: 200,
              ),
              Positioned(
                bottom: 10,
                left: 10,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                  color: Colors.black.withOpacity(0.6),
                  child: Text(
                    'Featured',
                    style: TextStyle(color: Colors.white, fontSize: 14),
                  ),
                ),
              ),
              Positioned(
                top: 5,
                right: 5,
                child: Icon(
                  Icons.star,
                  color: Colors.yellow,
                  size: 24,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

4. LayoutBuilder: Responding to Parent Constraints

LayoutBuilder is a powerful widget that allows you to build a widget tree based on the parent's incoming constraints. Unlike MediaQuery, which provides information about the entire screen, LayoutBuilder gives you the constraints of the widget's parent, enabling highly granular responsive layouts.

Key Property:

  • builder: A function that takes a BuildContext and BoxConstraints, returning the widget to be built.

Example:

This example demonstrates how to change the layout of a widget based on the available width provided by its parent.


import 'package:flutter/material.dart';

class LayoutBuilderExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('LayoutBuilder Example')),
      body: Center(
        child: Container(
          color: Colors.blueGrey[50],
          width: MediaQuery.of(context).size.width * 0.8, // 80% of screen width
          height: 150,
          child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
              if (constraints.maxWidth > 400) {
                return Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Icon(Icons.widgets, size: 50, color: Colors.blue),
                    Text('Wide Layout', style: TextStyle(fontSize: 24)),
                    ElevatedButton(onPressed: () {}, child: Text('Action')),
                  ],
                );
              } else {
                return Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Icon(Icons.widgets, size: 40, color: Colors.green),
                    Text('Narrow Layout', style: TextStyle(fontSize: 18)),
                    ElevatedButton(onPressed: () {}, child: Text('More')),
                  ],
                );
              }
            },
          ),
        ),
      ),
    );
  }
}

Combining for Creative UI

The true power of these widgets emerges when they are combined. Imagine a complex card UI: an image fills the background (Stack base), a gradient overlay sits on top (another Stack child), a title and description are precisely positioned at the bottom-left (Positioned), and an "add to favorites" button is placed at the top-right (another Positioned). Furthermore, the font size or even the arrangement of elements within the card could dynamically adjust based on the card's available width using LayoutBuilder.

For instance, to create a user profile header that adapts to screen size:


import 'package:flutter/material.dart';

class CreativeUIExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Creative UI Example')),
      body: LayoutBuilder(
        builder: (context, constraints) {
          bool isLargeScreen = constraints.maxWidth > 600;

          return Stack(
            alignment: Alignment.topCenter,
            children: [
              Container(
                height: isLargeScreen ? 250 : 200,
                color: Colors.deepPurple,
              ),
              Positioned(
                top: isLargeScreen ? 120 : 80,
                child: CircleAvatar(
                  radius: isLargeScreen ? 60 : 40,
                  backgroundImage: NetworkImage('https://picsum.photos/id/237/200/200'),
                  backgroundColor: Colors.white,
                ),
              ),
              Positioned(
                top: isLargeScreen ? 200 : 150,
                child: Column(
                  children: [
                    Text(
                      'Jane Doe',
                      style: TextStyle(
                        fontSize: isLargeScreen ? 28 : 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                      ),
                    ),
                    Text(
                      'Flutter Developer',
                      style: TextStyle(
                        fontSize: isLargeScreen ? 18 : 14,
                        color: Colors.white70,
                      ),
                    ),
                  ],
                ),
              ),
              Positioned(
                top: 10,
                right: 10,
                child: IconButton(
                  icon: Icon(Icons.settings, color: Colors.white),
                  onPressed: () {},
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

Conclusion

Align, Stack, Positioned, and LayoutBuilder are indispensable tools in a Flutter developer's arsenal for crafting sophisticated and responsive UIs. By mastering their individual capabilities and understanding how to combine them, you can move beyond basic linear layouts to build truly creative, dynamic, and pixel-perfect user experiences that adapt gracefully across different screen sizes and orientations.

Related Articles

May 14, 2026

Building a Multi-Event Countdown Timer Widget with Reminders, Notifications, Repeat, and Custom Labels in Flutter

Building a Multi-Event Countdown Timer Widget with Reminders, Notifications, Repeat, and Custom Labels in Flutter Countdown timers are essential in many applic

May 11, 2026

Unleashing Dynamic UIs: Flutter's Animation Prowess

Unleashing Dynamic UIs: Flutter's Animation Prowess for Slide & Scale Effects Flutter's declarative UI framework, combined with its powerful animation capabilit

May 11, 2026

Building a Product Detail Page Widget in Flutter with Related Items, Review Carousel, Promo Badges, and Quick Buy

Building a Product Detail Page Widget in Flutter with Related Items, Review Carousel, Promo Badges, and Quick Buy A well-designed Product Detail Page (PDP) is