text
stringlengths
1
372
this is more efficient than creating a new
FragmentShader for each frame.
for a more detailed guide on writing performant shaders,
check out writing efficient shaders on GitHub.
<topic_end>
<topic_start>
other resources
for more information, here are a few resources.
<topic_end>
<topic_start>
add interactivity to your flutter app
<topic_end>
<topic_start>
what you'll learn
how do you modify your app to make it react to user input?
in this tutorial, you’ll add interactivity to an app that
contains only non-interactive widgets.
specifically, you’ll modify an icon to make it tappable
by creating a custom stateful widget that manages two
stateless widgets.
the building layouts tutorial showed you how to create
the layout for the following screenshot.
when the app first launches, the star is solid red,
indicating that this lake has previously been favorited.
the number next to the star indicates that 41
people have favorited this lake. after completing this tutorial,
tapping the star removes its favorited status,
replacing the solid star with an outline and
decreasing the count. tapping again favorites the lake,
drawing a solid star and increasing the count.
to accomplish this, you’ll create a single custom widget
that includes both the star and the count,
which are themselves widgets. tapping the star changes state
for both widgets, so the same widget should manage both.
you can get right to touching the code in
step 2: subclass StatefulWidget.
if you want to try different ways of managing state,
skip to managing state.
<topic_end>
<topic_start>
stateful and stateless widgets
a widget is either stateful or stateless. if a widget can
change—when a user interacts with it,
for example—it’s stateful.
a stateless widget never changes.
icon, IconButton, and text are
examples of stateless widgets. stateless widgets
subclass StatelessWidget.
a stateful widget is dynamic: for example,
it can change its appearance in response to events
triggered by user interactions or when it receives data.
checkbox, radio, slider,
InkWell, form, and TextField
are examples of stateful widgets. stateful widgets
subclass StatefulWidget.
a widget’s state is stored in a state object,
separating the widget’s state from its appearance.
the state consists of values that can change, like a
slider’s current value or whether a checkbox is checked.
when the widget’s state changes,
the state object calls setState(),
telling the framework to redraw the widget.
<topic_end>
<topic_start>
creating a stateful widget
<topic_end>
<topic_start>
what's the point?
in this section, you’ll create a custom stateful widget.
you’ll replace two stateless widgets—the solid red
star and the numeric count next to it—with a single
custom stateful widget that manages a row with two
children widgets: an IconButton and text.
implementing a custom stateful widget requires creating two classes:
this section shows you how to build a stateful widget,
called FavoriteWidget, for the lakes app.
after setting up, your first step is choosing how state is
managed for FavoriteWidget.
<topic_end>
<topic_start>
step 0: get ready
if you’ve already built the app in the
building layouts tutorial,
skip to the next section.
once you have a connected and enabled device,
or you’ve launched the iOS simulator
(part of the flutter install) or the
android emulator (part of the android studio
install), you are good to go!
<topic_end>
<topic_start>
step 1: decide which object manages the widget’s state
a widget’s state can be managed in several ways,
but in our example the widget itself,
FavoriteWidget, will manage its own state.
in this example, toggling the star is an isolated
action that doesn’t affect the parent widget or the rest of
the UI, so the widget can handle its state internally.
learn more about the separation of widget and state,
and how state might be managed, in managing state.