text
stringlengths
1
474
final ValueChanged<bool> onChanged;
@override
State<TapboxC> createState() => _TapboxCState();
}
class _TapboxCState extends State<TapboxC> {
bool _highlight = false;
void _handleTapDown(TapDownDetails details) {
setState(() {
_highlight = true;
});
}
void _handleTapUp(TapUpDetails details) {
setState(() {
_highlight = false;
});
}
void _handleTapCancel() {
setState(() {
_highlight = false;
});
}
void _handleTap() {
widget.onChanged(!widget.active);
}
@override
Widget build(BuildContext context) {
// This example adds a green border on tap down.
// On tap up, the square changes to the opposite state.
return GestureDetector(
onTapDown: _handleTapDown, // Handle the tap events in the order that
onTapUp: _handleTapUp, // they occur: down, up, tap, cancel
onTap: _handleTap,
onTapCancel: _handleTapCancel,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: widget.active ? Colors.lightGreen[700] : Colors.grey[600],
border: _highlight
? Border.all(
color: Colors.teal[700]!,
width: 10,
)
: null,
),
child: Center(
child: Text(widget.active ? 'Active' : 'Inactive',
style: const TextStyle(fontSize: 32, color: Colors.white)),
),
),
);
}
}<code_end>
An alternate implementation might have exported the highlight
state to the parent while keeping the active state internal,
but if you asked someone to use that tap box,
they’d probably complain that it doesn’t make much sense.
The developer cares whether the box is active.
The developer probably doesn’t care how the highlighting
is managed, and prefers that the tap box handles those
details.<topic_end>
<topic_start>
Other interactive widgets
Flutter offers a variety of buttons and similar interactive widgets.
Most of these widgets implement the Material Design guidelines,
which define a set of components with an opinionated UI.If you prefer, you can use GestureDetector to build
interactivity into any custom widget.
You can find examples of GestureDetector in
Managing state. Learn more about the GestureDetector
in Handle taps, a recipe in the Flutter cookbook.lightbulb Tip
Flutter also provides a set of iOS-style widgets called
Cupertino.When you need interactivity, it’s easiest to use one of
the prefabricated widgets. Here’s a partial list:<topic_end>
<topic_start>
Standard widgets
<topic_end>
<topic_start>
Material Components
<topic_end>
<topic_start>
Resources
The following resources might help when adding interactivity
to your app.Gestures, a section in the Flutter cookbook.
<topic_end>
<topic_start>Taps, drags, and other gestures
This document explains how to listen for, and respond to,
gestures in Flutter.
Examples of gestures include taps, drags, and scaling.The gesture system in Flutter has two separate layers.
The first layer has raw pointer events that describe
the location and movement of pointers (for example,
touches, mice, and styli) across the screen.
The second layer has gestures that describe semantic
actions that consist of one or more pointer movements.<topic_end>
<topic_start>
Pointers
Pointers represent raw data about the user’s interaction
with the device’s screen.
There are four types of pointer events:On pointer down, the framework does a hit test on your app
to determine which widget exists at the location where the
pointer contacted the screen. The pointer down event