パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.2.0

after_layout

ウィジェットの最初のレイアウトが完了した後、つまり最初のフレームが表示された後にコードを実行します。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
24万
いいね数
778
Pub ポイント
160

パッケージをインストール

flutter pub add after_layout

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

after_layout パッケージは、ウィジェットの最初のレイアウト処理後にすぐにコードを実行できるようにし、ウィジェットが画面にレンダリングされた後の正確な寸法と位置にアクセスできるようにします。アニメーション、位置調整、または他の UI コンポーネントとの統合など、最終的なレイアウト測定値に依存する操作には特に役立ちます。このパッケージは、Flutter がサポートするすべての主要プラットフォーム(モバイル、Web、デスクトップ)で動作します。任意の StatefulWidget に追加できるミックスインを通じてシンプルな API を提供しています。

用途

  • レイアウト後のアニメーションを実行中
  • 実際のサイズに基づいてウィジェットの位置を調整する
  • レイアウトデータを必要とするサードパーティライブラリの初期化
  • レンダリング後のUI要素の同期
  • 動的コンテンツサイズの処理

主な機能

  • ミックスインベースのシンプルな API
  • クロスプラットフォーム互換性
  • 最初のフレームの後に一度実行する
  • タイマーまたはコールバックの必要はありません
  • StatefulWidget と連携可能

適している対象

  • 正確なレイアウトタイミングが必要な開発者
  • 動的なレイアウトを備えたUI重視のアプリケーション
  • レンダリング後にトリガーされるアニメーション
  • レイアウト認識を必要とするカスタムウィジェット

注意事項

  • 繰り返しのレイアウトイベントには適していません
  • 誤用するとパフォーマンス上の問題が発生する可能性があります
  • ステートフルウィジェットコンテキストが必要です
  • ウィジェットインスタンスごとに1回の実行に制限
  • 最初のビルドの前に動作しません

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

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト