12

Let's say that I have this widget:

Card(
  child: InkWell(
    onTap: () {},
    child: Padding(
      padding: const EdgeInsets.all(28.0),
      child: RaisedButton(
        child: Text('Test'),
        onPressed: () {},
      ),
    ),
  ),
),

I would like to disable (prevent showing) ripple effect on Card/InkWell only when the RaisedButton is tapped, but to show it when the Card is tapped (i.e. outside the button). Is there any way to achieve this effect?

I think the generalized question can be: How to prevent ripple effect on surrounding InkWell when tapped on InkWell inside? If you take a look at the source code then you can see that RaisedButton has Material and InkWell widgets that cause ripple.

Here is the full sample code.

enter image description here

3
  • 1
    @Eugene I want to show ripple when tapped on the Card itself Commented Oct 8, 2019 at 11:10
  • Have you tried IgnorePointer?
    – Tokenyet
    Commented Oct 8, 2019 at 11:11
  • @Tokenyet how would you recommend to use IgnorePointer? Commented Oct 8, 2019 at 11:13

3 Answers 3

7

If you want a quick hack, check it out:

Container(
  width: 180,
  height: 120,
  child: Stack(
    children: <Widget>[
      Card(
        child: InkWell(
          onTap: () {},
        ),
      ),
      Center(
        child: RaisedButton(
          child: Text('Test'),
          onPressed: () {},
        ),
      )
    ],
  ),
)

enter image description here

3
  • This works in this situation so solves the problem, but frankly I have a bit more complex widget tree so maybe someone has more robust solution. Either way thanks for the clever hack. Commented Oct 8, 2019 at 11:20
  • Dominik Roszkowski have you found a better solution? My widget tree is complex too. Commented Oct 14, 2020 at 10:45
  • This solution worked for me without any problem. thanks a lot. Commented Apr 22, 2023 at 21:40
2

enter image description here

You can handle the logic inside onHighlightChanged method;

Color _color;

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: Center(
      child: Card(
        child: InkWell(
          splashColor: _color,
          highlightColor: _color,
          onTap: () {},
          child: Padding(
            padding: const EdgeInsets.all(28.0),
            child: RaisedButton(
              onHighlightChanged: (value) {
                if (value) {
                  setState(() => _color = Colors.white);
                } else {
                  Timer(Duration(milliseconds: 200), () {
                    setState(() => _color = null);
                  });
                }
              },
              child: Text('Test'),
              onPressed: () {},
            ),
          ),
        ),
      ),
    ),
  );
}
2
  • overcomplicated and has artifacts at the edges
    – user10539074
    Commented Oct 8, 2019 at 11:23
  • @Eugene Haha, it could be but someone already posted using Stack so I used different approach, check out the updated screenshot, problem solved at edges.
    – CopsOnRoad
    Commented Oct 8, 2019 at 11:29
2

try this out https://dartpad.dev/7ff7f5756d93db2d4ed6a4b9a6d75208. I used hack with wrapping in InkWell the widget you want to surround with parent InkWell

Not the answer you're looking for? Browse other questions tagged or ask your own question.