text
stringlengths
1
474
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
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo'),
),
body: const Center(
child: TapboxA(),
),
),
);
}
}<code_end>
<topic_end>
<topic_start>
The parent widget manages the widget’s state
Often it makes the most sense for the parent widget
to manage the state and tell its child widget when to update.
For example, IconButton allows you to treat
an icon as a tappable button. IconButton is a
stateless widget because we decided that the parent
widget needs to know whether the button has been tapped,
so it can take appropriate action.In the following example, TapboxB exports its state
to its parent through a callback. Because TapboxB
doesn’t manage any state, it subclasses StatelessWidget.The ParentWidgetState class:The TapboxB class:
<code_start>import 'package:flutter/material.dart';