text
stringlengths
1
372
<topic_end>
<topic_start>
step 4: plug the stateful widget into the widget tree
add your custom stateful widget to the widget tree in
the app’s build() method. first, locate the code that
creates the icon and text, and delete it.
in the same location, create the stateful widget:
that’s it! when you hot reload the app,
the star icon should now respond to taps.
<topic_end>
<topic_start>
problems?
if you can’t get your code to run, look in your
IDE for possible errors. debugging flutter apps might help.
if you still can’t find the problem,
check your code against the interactive lakes example on GitHub.
if you still have questions, refer to any one of the developer
community channels.
the rest of this page covers several ways a widget’s state can
be managed, and lists other available interactive widgets.
<topic_end>
<topic_start>
managing state
<topic_end>
<topic_start>
what's the point?
who manages the stateful widget’s state? the widget itself?
the parent widget? both? another object?
the answer is… it depends. there are several valid ways
to make your widget interactive. you, as the widget designer,
make the decision based on how you expect your widget to be used.
here are the most common ways to manage state:
how do you decide which approach to use?
the following principles should help you decide:
if the state in question is user data,
for example the checked or unchecked
mode of a checkbox, or the position of a slider,
then the state is best managed by the parent widget.
if the state in question is aesthetic,
for example an animation, then the
state is best managed by the widget itself.
if in doubt, start by managing state in the parent widget.
we’ll give examples of the different ways of managing state
by creating three simple examples: TapboxA, TapboxB,
and TapboxC. the examples all work similarly—each
creates a container that, when tapped, toggles between a
green or grey box. the _active boolean determines the
color: green for active or grey for inactive.
these examples use GestureDetector to capture activity
on the container.
<topic_end>
<topic_start>
the widget manages its own state
sometimes it makes the most sense for the widget
to manage its state internally. for example,
ListView automatically scrolls when its
content exceeds the render box. most developers
using ListView don’t want to manage ListView’s
scrolling behavior, so ListView itself manages its scroll offset.
the _TapboxAState class:
<code_start>
import 'package:flutter/material.dart';
// TapboxA manages its own state.
//------------------------- TapboxA ----------------------------------
class TapboxA extends StatefulWidget {
const TapboxA({super.key});
@override
State<TapboxA> createState() => _TapboxAState();
}
class _TapboxAState extends State<TapboxA> {
bool _active = false;
void _handleTap() {
setState(() {
_active = !_active;
});
}
@override
widget build(BuildContext context) {
return GestureDetector(
onTap: _handleTap,
child: container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: _active ? Colors.lightGreen[700] : colors.grey[600],
),
child: center(
child: text(
_active ? 'active' : 'inactive',
style: const TextStyle(fontSize: 32, color: colors.white),
),
),
),
);
}
}
//------------------------- MyApp ----------------------------------
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override