搜索插件、代码仓与 APP 分类

插件详情v1.2.0

after_layout

在您的小部件完成首次布局后执行代码,即在第一帧显示后执行。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
23.4万
喜欢数
778
Pub 分数
160

安装插件

flutter pub add after_layout

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

after_layout 包使开发者能够在小部件首次布局后立即执行代码,确保在小部件渲染到屏幕上后能够访问准确的尺寸和位置。它特别适用于需要依赖最终布局测量值的操作,例如动画、位置调整或与其他 UI 组件集成。该包可在 Flutter 支持的所有主要平台上使用,包括移动设备、网页和桌面端。它通过一个混入(mixin)提供了一个简单的 API,可添加到任何 StatefulWidget 中。

适合场景

  • 运行布局后动画
  • 根据实际大小调整控件位置
  • 初始化需要布局数据的第三方库
  • 渲染后同步 UI 元素
  • 处理动态内容大小

主要特点

  • 基于 mixin 的简单 API
  • 跨平台兼容性
  • 在第一帧之后执行一次
  • 无需计时器或回调
  • 适用于 StatefulWidget

适合对象

  • 需要精确布局时间的开发人员
  • 具有动态布局的UI密集型应用程序
  • 渲染后触发的动画
  • 需要布局感知的自定义小部件

使用建议

  • 不适用于重复的布局事件
  • 如果使用不当,可能会导致性能问题
  • 需要有状态的部件上下文
  • 每个小部件实例仅限执行一次
  • 首次构建前无法使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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'),
            )
          ],
        );
      },
    );
  }
}

所属分类

源码仓库

使用此插件的项目