Sub topic 7
Ripple feedback with InkWell
This chapter covers how InkWell gives tappable areas Material ripple feedback and why it fails when its surroundings are wrong. It assumes you can build a widget tree with StatelessWidget and StatefulWidget, use setState, and lay out content with Padding, Row, Column and Stack. It also assumes you have met GestureDetector and its tap callbacks.
What InkWell adds to a tap
GestureDetector recognizes a tap but shows nothing when it happens. The user gets no sign that the app noticed the touch. InkWell is a Material widget that recognizes the same kinds of taps and also paints visual feedback:
- A ripple that spreads from the touch point (the splash).
- A soft overlay while the pointer is down or hovering (the highlight).
- A focus overlay for keyboard and d-pad navigation.
The Material buttons from earlier chapters use InkWell-style feedback internally. You reach for InkWell directly when you want a tappable card, list row, or image that looks like it belongs in a Material app but is not a button.
import 'package:flutter/material.dart';
class OpenCard extends StatelessWidget {
const OpenCard({super.key});
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: InkWell(
onTap: () => debugPrint('open'),
borderRadius: BorderRadius.circular(8),
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('Open'),
),
),
);
}
}The Material above the InkWell is not decoration. The next section explains why.
Why InkWell needs a Material ancestor
The ripple is not painted by the InkWell itself. InkWell asks the nearest Material ancestor for its ink layer and paints the splash there. The Material widget owns a canvas where ink effects are drawn, and everything between that canvas and the InkWell is painted on top of it.
This has two consequences.
- If there is no
Materialancestor, the ripple has nowhere to draw. In debug mode Flutter throws an assertion that noMaterialwidget was found. - If a widget between the
Materialand theInkWellpaints an opaque background, it covers the ink. The ripple is drawn, but you cannot see it.
The second case is the most common bug. A Container with a color or decoration wrapped around the InkWell's child is fine, but a Container with a color placed above the InkWell and below the Material hides the ripple.
// Ripple is hidden: the Container paints over the Material's ink layer.
Material(
child: Container(
color: Colors.white,
child: InkWell(
onTap: () {},
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('No visible ripple'),
),
),
),
)Put the color on the Material instead, as in the first example, so the ink is painted above it.
flowchart TDThe diagram below shows the paint order that explains the problem.
Where Material already exists
You often do not need to add a Material yourself. A Scaffold body is not a Material, but Card, ListTile, Dialog, and Drawer are, or contain one. MaterialApp plus Scaffold gives you a Material for the page background in many layouts, but its color can still be covered by your own widgets. When in doubt, wrap the area in Material(type: MaterialType.transparency), which supplies an ink layer without painting a background.
Core properties
| Property | Purpose |
|---|---|
| `onTap` | Called on a completed tap. Passing `null` for all callbacks disables the ink effects. |
| `onDoubleTap`, `onLongPress` | Same meaning as in `GestureDetector`. |
| `borderRadius` | Clips the splash and highlight to rounded corners. |
| `customBorder` | Clips to any `ShapeBorder`, such as `CircleBorder`. |
| `splashColor` | Color of the ripple. |
| `highlightColor` | Color of the pressed overlay. |
| `hoverColor`, `focusColor` | Overlay colors for mouse hover and keyboard focus. |
| `splashFactory` | Selects the splash style, for example `NoSplash.splashFactory` to remove the ripple. |
| `overlayColor` | A `WidgetStateProperty<Color?>` that replaces the color choices above. |
| `child` | The tappable content. |
Clipping the ripple
By default the ripple fills the rectangle of the InkWell. If the visible shape is rounded, the ripple spills past the corners unless you clip it. Match borderRadius to the shape you actually draw.
Material(
color: Colors.teal.shade50,
borderRadius: BorderRadius.circular(16),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: () {},
borderRadius: BorderRadius.circular(16),
child: const Padding(
padding: EdgeInsets.all(20),
child: Text('Rounded and clipped'),
),
),
)Here the Material supplies the rounded background and the InkWell uses the same radius so the splash stays inside. For a circular target use customBorder: const CircleBorder().
Changing colors with overlayColor
overlayColor takes a WidgetStateProperty, the same mechanism used for button styling. It lets you choose a color per interaction state.
Material(
type: MaterialType.transparency,
child: InkWell(
onTap: () {},
overlayColor: WidgetStateProperty.resolveWith<Color?>((states) {
if (states.contains(WidgetState.pressed)) {
return Colors.teal.withOpacity(0.25);
}
if (states.contains(WidgetState.hovered)) {
return Colors.teal.withOpacity(0.10);
}
return null;
}),
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('Custom overlay'),
),
),
)Returning null means "no overlay for this state".
Ink: ripples over an image or colored child
Sometimes the content needs its own background, such as an image or a gradient, and the ripple must show above it. A plain Container would cover the ink. The Ink widget solves this by painting its decoration on the Material's ink layer too, so ink effects from the InkWell draw above it in the correct order.
Material(
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(
'Ink keeps the ripple visible',
style: TextStyle(color: Colors.white),
),
),
),
),
)Use Ink for backgrounds that sit directly around an InkWell. Note that Ink has no alignment or margin; wrap it in Padding or Center if you need those.
InkWell versus GestureDetector
| Question | InkWell | GestureDetector |
|---|---|---|
| Visual feedback | Ripple, highlight, hover, focus | None |
| Needs Material ancestor | Yes, for the ink | No |
| Keyboard focus and activation | Built in (focusable, Enter and Space activate it) | Not provided |
| Drag, pan, scale callbacks | No | Yes |
| Typical use | Tappable cards and rows | Custom gestures and invisible hit areas |
Choose InkWell for tap-style actions in a Material UI. Choose GestureDetector when you need drags or scale, or when the feedback should look different from Material. You can nest them, but keep in mind the gesture arena chapter: the two compete for the same pointer.
Common errors
- No ripple appears. Check for a missing
Material, or an opaqueContainerorDecoratedBoxbetween theMaterialand theInkWell. Switch toInkor move the color up to theMaterial. - The ripple extends past rounded corners. Set
borderRadiusorcustomBorderon theInkWellto match the shape. - The ripple covers only part of the content. The
InkWelltakes the size of its child. If the child is a smallText, only that area responds. AddPaddinginside theInkWell, not outside it, so the padding is part of the tappable area. - Nothing responds and no ripple shows. All callbacks are
null, so theInkWellis disabled. Provide at least one callback. - A child button swallows the tap. A
TextButtoninside anInkWellhandles its own taps first, so the outeronTapnever fires for that area.
// Padding outside: only the Text is tappable.
Padding(
padding: const EdgeInsets.all(16),
child: InkWell(onTap: () {}, child: const Text('Small target')),
)
// Padding inside: the whole padded area is tappable.
InkWell(
onTap: () {},
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('Large target'),
),
)Exercises
Exercise
A tappable row wraps an InkWell in a Container with color: Colors.white, and that Container sits directly under a Material. Taps work, but no ripple is visible. Why, and how do you fix it?
Show answer
The InkWell paints its splash on the Material's ink layer, and the opaque Container sits between the Material and the InkWell, so it paints over the splash. Remove the Container's color and set the color on the Material, or replace the Container with an Ink widget that has the same decoration so it draws on the ink layer below the splash.
Exercise
You build a card with BorderRadius.circular(12) and put an InkWell inside it. The ripple shows square corners when you press near an edge. What property do you add, and where?
Show answer
Add borderRadius: BorderRadius.circular(12) to the InkWell, matching the card's radius. Also give the Material clipBehavior: Clip.antiAlias if the Material itself draws the rounded background.