搜尋套件、儲存庫與 APP 分類

套件詳情v1.2.0

after_layout

在您的組件完成首次佈局後執行程式碼,也就是在第一幀顯示後執行。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
24.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'),
            )
          ],
        );
      },
    );
  }
}

所屬分類

原始碼儲存庫

使用此套件的專案