InkWell and GestureDetector, how to make them work?

flutter inkwell
flutter inkwell background color
gesturedetector flutter
flutter gesturedetector( ontap not working)
flutter gesturedetector drag
flutter gesturedetector swipe
flutter card ontap
flutter inkresponse

I'd like to use a GestureDetector for it's onTapDown callback but also have a nice InkWell splash effect.

Is it possible to use these two together?

If you want to unconditionally handle the pointer down event with no gesture disambiguation, you can make the InkWell a child of a Listener, and set the onPointerDown handler.

For example:

new Listener(
  onPointerDown: (e) { print('onPointerDown'); },
  child: new InkWell(
    child: new Text('Tap me'),
    onTap: () { print('onTap'); }
  ),
),

It might make sense to add an onTapDown handler to InkWell.

InkWell class - material library - Dart API, widget must have a Material widget as an ancestor. The Material widget is where the ink reactions are actually painted. Flutter Tutorial - Flutter GestureDetector and InkWell In this video, you will see how to make Images, Text, Icons, Buttons Clickable using GestureDetector or InkWell. If you want to learn the UI

You can pass a HitTestBehavior to GestureDetector to make it "non-blocking" by setting the value to "translucent"

Or you can also trigger it yourself using Material.of(context)

Flutter - Introduction to Gestures, will capture the gesture and dispatch multiple events based on the gesture. By default a GestureDetector with an invisible child ignores touches; this behavior can be controlled with behavior. GestureDetector also listens for accessibility events and maps them to the callbacks. To ignore accessibility events, set excludeFromSemantics to true. See flutter.dev/gestures/ for additional information.

InkWell uses a GestureDetector under it's hood:

https://github.com/flutter/flutter/blob/79b5e5bc8af7d9df3374dfe6141653848d1c03ac/packages/flutter/lib/src/material/ink_well.dart#L560

The reason why a GestureDetector isn't able to work well if the InkWell-Widget is, that it sets the behavior on it's internal GestureDetector to "HitTestBehavior.opaque". This prevents "event bubbling" / event capturing on parent widgets (if my understanding is correct). And because the "behavior" is final, we can't change / fix that by our own.

https://github.com/flutter/flutter/blob/79b5e5bc8af7d9df3374dfe6141653848d1c03ac/packages/flutter/lib/src/material/ink_well.dart#L566

As I mentioned in a comment above, I wrapped the InkWell within a widget (which handles other stuff too) and provided a way to pass a callback into it which is executed on the desired event.

This is my example solution:

import 'package:flutter/material.dart';

class CardPreview extends StatefulWidget {   

  final dynamic data;   
  final VoidCallback onTap;

  const CardPreview(this.data, this.onTap);

  CardPreview._(this.data, this.onTap);

  factory CardPreview.fromData(dynamic data, VoidCallback onTap) {
    return new CardPreview(data, onTap);
  }

  CardPreview createState() => new CardPreview(this.data, this.onTap);
}

class CardPreviewState extends State<CardPreview> {   
  final dynamic data;   
  final VoidCallback onTap;

  CardPreviewState(this.data, this.onTap);

  Widget buildCard() {
    return Material(
      color: Colors.transparent,
      child: Ink(
        child: InkWell(
          child: null,
          onTap: () {
            if (this.onTap == null) {
              return;
            }

            this.onTap();
          },
        ),
      ),
    );  
 }

  @override   
  Widget build(BuildContext context) {
    return Container(
      child: buildCard(),
    );   
  } 
}

Flutter: How do you make a card clickable?, of the user, creates the rebound effect with the chosen color, on the card .. Widgets that follow the Material Design guidelines display a ripple animation when tapped. Flutter provides the InkWell widget to perform this effect. Create a ripple effect using the following steps: Create a widget that supports tap. Wrap it in an InkWell widget to manage tap callbacks and ripple animations.

Flutter Tutorial - Flutter GestureDetector and InkWell, Flutter Tutorial - Flutter GestureDetector and InkWell In this video, you will see how to make Duration: 13:38 Posted: Feb 8, 2019 Here, a Text(Gesture) included as a child of the GestureDetector widget. Inside the onTap event, value of name changed into “Text by GestureDetector”. InkWell. InkWell is similar to GestureDetector, you can add any widgets as the child of the InkWell widget.

Programming Flutter: Native, Cross-Platform Apps the Easy Way, The Ink widget[42] makes that simpler: it provides a Ink.image method that allows you to set the InkWell as a child, making the image and the Material on top of it an correctly shows the inksplash over the image and that works when replacing inskplash effect at all, you can use a GestureDetector instead of the InkWell. It  I faced a similar issue where the child of the PopupMenuButton would have a square InkWell around it. In order to make it behave like an IconButton, which naturally uses the rounded InkWell, I simply had to use the icon paramater instead of the child. icon: Icon(Icons.more_vert), This is indicated in the documentation for that paramater:

Our Gestures doc implies using an InkWell if you want to make , Working with our tech writers, we were confused about the following a gesture detector directly, consider using an InkWell to listen to taps, and. Specifically, the "InkWell to listen to taps" it implies that if you want to make  I am trying to implement and inkWell wrap on a card widget, but it does not work at all. I am leaving on tap as null because this class takes 3 arguments that I populate later on to generate multiple cards. I cannot see what is going on that is preventing InkWell from rippling, so any help would be appreciated.

Comments
  • I know I'm a little late to the party, but I had the same issue. I ended up to use the InkWell widget directly. Under the hood it returns a GestureDetector as well. In my widget I made a constructor which accepts a VoidCallback onTap. Within the InkWell onTap-Event I just call this provided callback.