text
stringlengths
1
474
that needs to happen just once. For example, override initState
to configure animations or to subscribe to platform services.
Implementations of initState are required to start
by calling super.initState.When a state object is no longer needed,
the framework calls dispose() on the state object.
Override the dispose function to do cleanup work.
For example, override dispose to cancel timers or to
unsubscribe from platform services. Implementations of
dispose typically end by calling super.dispose.For more information, check out State.<topic_end>
<topic_start>
Keys
Use keys to control which widgets the framework matches up
with other widgets when a widget rebuilds. By default, the
framework matches widgets in the current and previous build
according to their runtimeType and the order in which they appear.
With keys, the framework requires that the two widgets have
the same key as well as the same runtimeType.Keys are most useful in widgets that build many instances of
the same type of widget. For example, the ShoppingList widget,
which builds just enough ShoppingListItem instances to
fill its visible region:Without keys, the first entry in the current build
would always sync with the first entry in the previous build,
even if, semantically, the first entry in the list just
scrolled off screen and is no longer visible in the viewport.By assigning each entry in the list a “semantic” key,
the infinite list can be more efficient because the
framework syncs entries with matching semantic keys
and therefore similar (or identical) visual appearances.
Moreover, syncing the entries semantically means that
state retained in stateful child widgets remains attached
to the same semantic entry rather than the entry in the
same numerical position in the viewport.For more information, check out the Key API.<topic_end>
<topic_start>
Global keys
Use global keys to uniquely identify child widgets.
Global keys must be globally unique across the entire
widget hierarchy, unlike local keys which need
only be unique among siblings. Because they are
globally unique, a global key can be used to
retrieve the state associated with a widget.For more information, check out the GlobalKey API.
<topic_end>
<topic_start>Widget catalog
Create beautiful apps faster with Flutter’s collection of visual, structural,
platform, and interactive widgets. In addition to browsing widgets by category,
you can also see all the widgets in the widget index.Make your app accessible.Bring animations to your app.Manage assets, display images, and show icons.Async patterns to your
Flutter application.Widgets you absolutely need to know before building your first Flutter app.Beautiful and high-fidelity widgets for current iOS design language.Take user
input in addition to input widgets in Material components and Cupertino.Respond to touch events and route users to different views.Arrange other widgets columns, rows, grids,
and many other layouts.Widgets implementing the Material 2 Design guidelines.Visual, behavioral, and motion-rich widgets implementing the Material 3 design specification.Material 3
is the default Flutter interface as of Flutter 3.16. To learn more about this transition, check out Flutter support for Material 3.These widgets apply visual effects to the children
without changing their layout, size, or position.Scroll multiple widgets as children of the parent.Manage the theme of your app, makes your app responsive to screen sizes, or add padding.Display and style text.<topic_end>
<topic_start>Widget of the Week
100+ short, 1-minute explainer videos to
help you quickly get started with Flutter widgets.See more Widget of the Weeks
<topic_end>
<topic_start>Layouts in Flutter
<topic_end>
<topic_start>What's the point?
The core of Flutter’s layout mechanism is widgets.
In Flutter, almost everything is a widget—even
layout models are widgets. The images, icons,
and text that you see in a Flutter app are all widgets.
But things you don’t see are also widgets,
such as the rows, columns, and grids that arrange,
constrain, and align the visible widgets.You create a layout by composing widgets to build more complex widgets.
For example, the first screenshot below shows 3 icons with a label
under each one:The second screenshot displays the visual layout, showing a row of
3 columns where each column contains an icon and a label.info Note
Most of the screenshots in this tutorial are displayed with
debugPaintSizeEnabled set to true so you can see the visual layout.
For more information, see
Debugging layout issues visually, a section in
Using the Flutter inspector.Here’s a diagram of the widget tree for this UI:Most of this should look as you might expect, but you might be wondering
about the containers (shown in pink). Container is a widget class
that allows you to customize its child widget. Use a Container when
you want to add padding, margins, borders, or background color,
to name some of its capabilities.In this example, each Text widget is placed in a Container
to add margins. The entire Row is also placed in a
Container to add padding around the row.The rest of the UI in this example is controlled by properties.
Set an Icon’s color using its color property.
Use the Text.style property to set the font, its color, weight, and so on.
Columns and rows have properties that allow you to specify how their
children are aligned vertically or horizontally, and how much space
the children should occupy.<topic_end>
<topic_start>
Lay out a widget
How do you lay out a single widget in Flutter? This section
shows you how to create and display a simple widget.
It also shows the entire code for a simple Hello World app.In Flutter, it takes only a few steps to put text, an icon,
or an image on the screen.<topic_end>
<topic_start>
1. Select a layout widget
Choose from a variety of layout widgets based
on how you want to align or constrain the visible widget,
as these characteristics are typically passed on to the
contained widget.This example uses Center which centers its content
horizontally and vertically.<topic_end>
<topic_start>
2. Create a visible widget
For example, create a Text widget:
<code_start>Text('Hello World'),<code_end>
Create an Image widget:
<code_start>return Image.asset(