Search packages, repositories and app categories

Package detailsv1.2.0

after_layout

Execute code after the first layout of your widget has been performed, i.e. after the first frame has been displayed.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
234K
Likes
778
Pub points
160

Install package

flutter pub add after_layout

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The after_layout package enables developers to execute code immediately after a widget's first layout pass, ensuring access to accurate dimensions and positions once the widget has been rendered on screen. It is particularly useful for performing actions that depend on final layout measurements, such as animations, positioning adjustments, or integrating with other UI components. The package works across all major platforms supported by Flutter, including mobile, web, and desktop. It provides a simple API through a mixin that can be added to any StatefulWidget.

Use cases

  • Running post-layout animations
  • Adjusting widget positions based on actual size
  • Initializing third-party libraries requiring layout data
  • Synchronizing UI elements after rendering
  • Handling dynamic content sizing

Key features

  • Simple mixin-based API
  • Cross-platform compatibility
  • Executes once after first frame
  • No need for timers or callbacks
  • Works with StatefulWidgets

Suitable for

  • Developers needing precise layout timing
  • UI-heavy applications with dynamic layouts
  • Animations triggered after rendering
  • Custom widgets requiring layout awareness

Considerations

  • Not suitable for repeated layout events
  • May cause performance issues if misused
  • Requires stateful widget context
  • Limited to one execution per widget instance
  • Does not work before first build

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Flutter Community: after_layout

Flutter After Layout

pub package

Brings functionality to execute code after the first layout of a widget has been performed, i.e. after the first frame has been displayed.

Quick Usage

Add with AfterLayoutMixin<MyWidget> mixin to your State<MyWidget> class and then implement the FutureOr<void> afterFirstLayout(BuildContext context) abstract method. Code in this method will be called the first time this widget is laid out on the screen.

Motivation

If you want to display a widget that depends on the layout, such as a Dialog or BottomSheet, you can not use that widget in initState.

You might have tried this.

BAD CODE

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

@immutable
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'After Layout - Good Example',
      home: HomeScreen(),
    );
  }
}

@immutable
class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
    // NOTE: Calling this function here would crash the app.
    showHelloWorld();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(color: Colors.red),
    );
  }

  void showHelloWorld() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: const Text('Hello World'),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: const Text('DISMISS'),
            )
          ],
        );
      },
    );
  }
}

Usage

This demo showcases how this package resolves the shortcomings shown above:

GOOD CODE

import 'package:flutter/material.dart';
import 'package:after_layout/after_layout.dart';

void main() => runApp(const MyApp());

@immutable
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'After Layout - Good Example',
      home: HomeScreen(),
    );
  }
}

@immutable
class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> with AfterLayoutMixin<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(color: Colors.red),
    );
  }

  @override
  void afterFirstLayout(BuildContext context) {
    // Calling the same function "after layout" to resolve the issue.
    showHelloWorld();
  }

  void showHelloWorld() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: const Text('Hello World'),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: const Text('DISMISS'),
            )
          ],
        );
      },
    );
  }
}

Categories

Source repository

Projects using this package