{"id":13549240,"url":"https://github.com/fluttercommunity/flutter_after_layout","last_synced_at":"2025-04-12T22:22:53.095Z","repository":{"id":31988602,"uuid":"131225722","full_name":"fluttercommunity/flutter_after_layout","owner":"fluttercommunity","description":"Flutter After Layout - Brings functionality to execute code after the first layout of a widget has been performed, i.e. after the first frame has been displayed. Maintainer: @slightfoot","archived":false,"fork":false,"pushed_at":"2022-05-16T00:59:36.000Z","size":29,"stargazers_count":477,"open_issues_count":0,"forks_count":38,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T01:24:10.959Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/after_layout","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fluttercommunity.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-27T00:47:01.000Z","updated_at":"2025-02-26T04:48:57.000Z","dependencies_parsed_at":"2022-08-09T08:36:38.183Z","dependency_job_id":null,"html_url":"https://github.com/fluttercommunity/flutter_after_layout","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercommunity%2Fflutter_after_layout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercommunity%2Fflutter_after_layout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercommunity%2Fflutter_after_layout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercommunity%2Fflutter_after_layout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fluttercommunity","download_url":"https://codeload.github.com/fluttercommunity/flutter_after_layout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248638608,"owners_count":21137690,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:01:19.753Z","updated_at":"2025-04-12T22:22:53.037Z","avatar_url":"https://github.com/fluttercommunity.png","language":"Dart","funding_links":[],"categories":["Dart"],"sub_categories":[],"readme":"[![Flutter Community: after_layout](https://fluttercommunity.dev/_github/header/after_layout)](https://github.com/fluttercommunity/community)\n\n# Flutter After Layout\n\n[![pub package](https://img.shields.io/pub/v/after_layout.svg)](https://pub.dartlang.org/packages/after_layout)\n\nBrings functionality to execute code after the first layout of a widget has been performed, i.e. after the first frame has been displayed.\n\n\n## Quick Usage\n\nAdd `with AfterLayoutMixin\u003cMyWidget\u003e` mixin to your `State\u003cMyWidget\u003e` class and then implement the `FutureOr\u003cvoid\u003e afterFirstLayout(BuildContext context)` abstract method. Code in this method will be called the first time this widget is laid out on the screen.\n\n\n## Motivation\nIf 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`.\n\nYou might have tried this.\n\n**BAD CODE**\n```dart\nimport 'package:flutter/material.dart';\n\nvoid main() =\u003e runApp(const MyApp());\n\n@immutable\nclass MyApp extends StatelessWidget {\n  const MyApp({Key? key}) : super(key: key);\n\n  @override\n  Widget build(BuildContext context) {\n    return const MaterialApp(\n      title: 'After Layout - Good Example',\n      home: HomeScreen(),\n    );\n  }\n}\n\n@immutable\nclass HomeScreen extends StatefulWidget {\n  const HomeScreen({Key? key}) : super(key: key);\n\n  @override\n  HomeScreenState createState() =\u003e HomeScreenState();\n}\n\nclass HomeScreenState extends State\u003cHomeScreen\u003e {\n  @override\n  void initState() {\n    super.initState();\n    // NOTE: Calling this function here would crash the app.\n    showHelloWorld();\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      body: Container(color: Colors.red),\n    );\n  }\n\n  void showHelloWorld() {\n    showDialog(\n      context: context,\n      builder: (BuildContext context) {\n        return AlertDialog(\n          content: const Text('Hello World'),\n          actions: \u003cWidget\u003e[\n            TextButton(\n              onPressed: () =\u003e Navigator.of(context).pop(),\n              child: const Text('DISMISS'),\n            )\n          ],\n        );\n      },\n    );\n  }\n}\n```\n\n\n## Usage\n\nThis demo showcases how this package resolves the shortcomings shown above:\n\n**GOOD CODE**\n\n```dart\nimport 'package:flutter/material.dart';\nimport 'package:after_layout/after_layout.dart';\n\nvoid main() =\u003e runApp(const MyApp());\n\n@immutable\nclass MyApp extends StatelessWidget {\n  const MyApp({Key? key}) : super(key: key);\n\n  @override\n  Widget build(BuildContext context) {\n    return const MaterialApp(\n      title: 'After Layout - Good Example',\n      home: HomeScreen(),\n    );\n  }\n}\n\n@immutable\nclass HomeScreen extends StatefulWidget {\n  const HomeScreen({Key? key}) : super(key: key);\n\n  @override\n  HomeScreenState createState() =\u003e HomeScreenState();\n}\n\nclass HomeScreenState extends State\u003cHomeScreen\u003e with AfterLayoutMixin\u003cHomeScreen\u003e {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      body: Container(color: Colors.red),\n    );\n  }\n\n  @override\n  void afterFirstLayout(BuildContext context) {\n    // Calling the same function \"after layout\" to resolve the issue.\n    showHelloWorld();\n  }\n\n  void showHelloWorld() {\n    showDialog(\n      context: context,\n      builder: (BuildContext context) {\n        return AlertDialog(\n          content: const Text('Hello World'),\n          actions: \u003cWidget\u003e[\n            TextButton(\n              onPressed: () =\u003e Navigator.of(context).pop(),\n              child: const Text('DISMISS'),\n            )\n          ],\n        );\n      },\n    );\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttercommunity%2Fflutter_after_layout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluttercommunity%2Fflutter_after_layout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttercommunity%2Fflutter_after_layout/lists"}