Sub topic 2
Icon buttons and tooltips
This chapter assumes you know Dart closures, StatefulWidget with setState, and layout widgets such as Row, Column, Stack and Padding. It covers how Flutter turns pointer input into actions.
Icon buttons and tooltips
An IconButton is a Material button that shows only an icon. It runs onPressed when tapped. Set tooltip so the button has a spoken label for screen readers and shows a hint on long press.
import 'package:flutter/material.dart';
class FavoriteButton extends StatelessWidget {
const FavoriteButton({super.key});
@override
Widget build(BuildContext context) {
return IconButton(
tooltip: 'Add to favorites',
icon: const Icon(Icons.favorite_border),
onPressed: () {},
);
}
}Detecting taps, double taps and long presses with GestureDetector
A GestureDetector recognizes gestures on its child but paints nothing itself. Each callback is optional.
GestureDetector(
onTap: () => debugPrint('tap'),
onDoubleTap: () => debugPrint('double tap'),
onLongPress: () => debugPrint('long press'),
child: const SizedBox(
width: 120,
height: 120,
child: Center(child: Text('Tap me')),
),
)When you set onDoubleTap, Flutter waits briefly after a tap to see whether a second tap follows, so onTap fires with a small delay.
Ripple feedback with InkWell
An InkWell works like a GestureDetector and also paints a Material ink ripple. The ripple is drawn on the nearest Material ancestor, so wrap the InkWell in a Material widget. Without one, Flutter throws an error at runtime.
Material(
color: Colors.white,
child: InkWell(
onTap: () {},
borderRadius: BorderRadius.circular(8),
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('Open'),
),
),
)Hit testing and HitTestBehavior
When a pointer goes down, Flutter runs a hit test. It starts at the top of the widget tree and collects every widget under the pointer. The gesture then goes to those widgets. A GestureDetector only receives hits where its child paints something. Set behavior: HitTestBehavior.opaque to make the whole box respond, including transparent areas.
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => debugPrint('tap'),
child: const SizedBox(width: 120, height: 120),
)Tracking drags with pan callbacks
onPanUpdate reports details.delta, the finger movement since the last update. Add the delta to a stored position and call setState to move the widget.
import 'package:flutter/material.dart';
class DraggableBox extends StatefulWidget {
const DraggableBox({super.key});
@override
State<DraggableBox> createState() => _DraggableBoxState();
}
class _DraggableBoxState extends State<DraggableBox> {
Offset _position = const Offset(40, 40);
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
left: _position.dx,
top: _position.dy,
child: GestureDetector(
onPanUpdate: (details) =>
setState(() => _position += details.delta),
child: Container(width: 80, height: 80, color: Colors.indigo),
),
),
],
);
}
}Exercise
You want a card to show a ripple when tapped. Which widget do you use, and what must surround it?
Show answer
Use an InkWell with an onTap callback, and place it inside a Material widget so the ripple has a Material ancestor to paint on.