Flutter: How do you make a card clickable?

Material DesignFlutter

Material Design Problem Overview


I just have a simple Card like new Card(child: new Text('My cool card')) and I want to be able to click anywhere on it to run some function, except there's no onPressed method for a Card. I could add a button to the bottom, but that's not ideal for this situation.

Anyone know how to make the whole card clickable?

Material Design Solutions


Solution 1 - Material Design

Flutter use composition over properties. Wrap the desired widget into a clickable one to achieve what you need.

Some clickable widgets : GestureDetector, InkWell, InkResponse.

GestureDetector(
  onTap: () => ......,
  child: Card(...),
);

Solution 2 - Material Design

Flutter provides the InkWell Widget. by registering a callback you can decide what happens when user clicks on the card (called tap in flutter). InkWell also implements Material Design ripple effect

Card(
  child: new InkWell(
    onTap: () {
      print("tapped");
    },
    child: Container(
      width: 100.0,
      height: 100.0,
    ),
  ),
),

Solution 3 - Material Design

I think you can also use InkWell apart from GestureDetector just wrap the card inside InkWell() Widget

InkWell(
  onTap: (){ print("Card Clicked"); }
  child: new Card(),
);

Solution 4 - Material Design

You can use Inkwell and insert splashColor which, at the click of the user, creates the rebound effect with the chosen color, on the card .. This is mainly used in material design.

return Card(
  color: item.completed ? Colors.white70 : Colors.white,
  elevation: 8,
  child: InkWell(
      splashColor: "Insert color when user tap on card",
      onTap: () async {
       
      },
    ),
);

Solution 5 - Material Design

In Flutter, InkWell is a material widget that responds to touch action.

InkWell(
    child: Card(......),
    onTap: () { 
        print("Click event on Container"); 
    },
);

GestureDetector is a widget that detects the gestures.

GestureDetector(
    onTap: () { 
        print("Click event on Container"); 
    },
    child: Card(.......),
)

Difference

InkWell is a material widget and it can show you a Ripple Effect whenever a touch was received.

GestureDetector is more general-purpose, not only for touch but also for other gestures.

Solution 6 - Material Design

Wrap a card in GestureDetector Widget like a below:

 GestureDetector(
    onTap: () {
      // To do
    },
    child: Card(
     
    ),
  ),

Another way is as follows:

InkWell(
    onTap: () {
      // To do
    },
    child: Card(),
  ),

Solution 7 - Material Design

The most preferred way is to add ListTile as Card child. Not only does ListTile contain the method onTap it also helps you in making Card interesting.

Card(
  child: ListTile(
    title: Text('Title')
    leading: CircleAvatar(
      backgroundImage: AssetImage('assets/images/test.jpg'),
    ),
    onTap: () {
      print('Card Clicked');
    },
  ),
),

Solution 8 - Material Design

Do something on tap/click of 'child' in Flutter:-

Code:-your code looks like:

child: Card(------
------------
--------),

Step1:- Put your mouse cursor on Card then, press- Alt+Enter(in windows) select wrap with widget.

Step2:- Change your default widget into GestureDetector.

final code:-

child: GestureDetector(
onTap: YourOnClickCode(),
child: Card(------
------------
--------),
),

Solution 9 - Material Design

You also can insert a card into a TextButton:

TextButton clickableCard = TextButton(child: card, onPressed: onCardClick, style: [...]);

This brings the advantage, that you get some features for free. For example in Flutter Web, you get a mousover effect and the cursor changes to the hand so that ths user knows, he can click there. Other additional features can be customised using the style.

Solution 10 - Material Design

Most of the answers are brilliant but I just want to share mine for the one who wants to make/show a ripple effect on Tap of card or list tile.

Card(
  child: TextButton(
    onPressed: ()=> ...,
    child: ListTile(
           title: Text('title'),
  ),
  ),
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJus10View Question on Stackoverflow
Solution 1 - Material DesignRémi RousseletView Answer on Stackoverflow
Solution 2 - Material DesignJossef Harush KadouriView Answer on Stackoverflow
Solution 3 - Material DesignMahesh JamdadeView Answer on Stackoverflow
Solution 4 - Material DesignAlexPadView Answer on Stackoverflow
Solution 5 - Material DesignJitesh MohiteView Answer on Stackoverflow
Solution 6 - Material DesignKalpesh KhandlaView Answer on Stackoverflow
Solution 7 - Material DesignWeb Developer in PuneView Answer on Stackoverflow
Solution 8 - Material Designayush bView Answer on Stackoverflow
Solution 9 - Material DesignAzureIPView Answer on Stackoverflow
Solution 10 - Material DesignMuhammad TalhaView Answer on Stackoverflow