Sub topic 7
Ripple Feedback with InkWell
Assume you know Flutter widget trees, StatefulWidget, callbacks, and basic layout widgets. This chapter focuses on visual feedback for custom tappable areas. Use InkWell when a tap target should look like part of a Material design surface and show a ripple when the user touches it.
Why use InkWell?
A button widget already provides tap handling, styling, accessibility, and Material feedback. InkWell is useful when you need a custom layout, such as a tappable card, list row, or decorated panel.
InkWell is a widget that detects taps and paints an interaction effect. Its effects include:
- A brief ripple, called a splash, when the user taps.
- A highlight while the pointer is over the target or the target is pressed.
- Optional callbacks such as
onTap,onDoubleTap, andonLongPress.
The callback is nullable. If onTap is null, the InkWell does not respond to taps.
The Material ancestor
InkWell paints its splash on the nearest Material ancestor. Material is the widget that provides a surface where Material effects can be drawn.
import 'package:flutter/material.dart';
class OpenCard extends StatelessWidget {
const OpenCard({super.key});
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
child: InkWell(
onTap: () {
debugPrint('Card opened');
},
borderRadius: BorderRadius.circular(12),
child: const Padding(
padding: EdgeInsets.all(20),
child: Text('Open details'),
),
),
);
}
}The Material widget supplies the surface. The InkWell supplies the gesture recognition and ripple. borderRadius keeps the effect inside the rounded shape.
Ink, decoration, and clipping
A frequent mistake is placing a colored or decorated Container directly above InkWell. The decoration can cover the splash because the splash is painted on the Material surface below it.
Use Ink for decorations that should appear below the splash:
Material(
borderRadius: BorderRadius.circular(12),
child: Ink(
decoration: BoxDecoration(
color: Colors.indigo,
borderRadius: BorderRadius.circular(12),
),
child: InkWell(
onTap: () {},
borderRadius: BorderRadius.circular(12),
child: const Padding(
padding: EdgeInsets.all(20),
child: Text(
'Continue',
style: TextStyle(color: Colors.white),
),
),
),
),
)If a parent clips its children, make sure the clipping shape matches the InkWell shape. Otherwise, the ripple may extend beyond the visible card or be clipped unexpectedly.
How a tap travels through the tree
The pointer first undergoes hit testing. Flutter identifies render objects under the pointer and sends the event to interested gesture recognizers. InkWell participates in this process, recognizes the tap, and then invokes onTap.
Common errors
- No visible ripple: Add a
Materialancestor and check that another opaque decoration is not covering the splash. - Ripple outside rounded corners: Give both
MaterialandInkWellthe sameborderRadius, and clip when the surrounding layout requires it. - Tap target too small: Add padding around the content so the interactive area is large enough.
- Unexpected competing gestures: A parent scrollable or another recognizer may compete for the same pointer sequence.
Exercise
Why should a decorated custom card usually use Ink inside Material instead of placing a decorated Container above InkWell?
Show answer
Ink paints the decoration on the Material surface below the splash, so the ripple remains visible instead of being covered by the Container.