[英]Flutter: How do you make a card clickable?
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.我只有一个简单的卡片,比如
new Card(child: new Text('My cool card'))
并且我希望能够点击它上的任何地方来运行一些功能,除了卡片没有onPressed
方法。 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?有谁知道如何使整个卡片可点击?
Flutter use composition over properties. Flutter 使用组合而不是属性。 Wrap the desired widget into a clickable one to achieve what you need.
将所需的小部件包装成可点击的小部件以实现您的需求。
Some clickable widgets : GestureDetector
, InkWell
,InkResponse
.一些可点击的小部件:
GestureDetector
、 InkWell
、InkResponse
。
GestureDetector(
onTap: () => ......,
child: Card(...),
);
Flutter provides the InkWell Widget. Flutter 提供了InkWell小部件。 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
InkWell还实现了 Material Design 涟漪效应
Card(
child: new InkWell(
onTap: () {
print("tapped");
},
child: Container(
width: 100.0,
height: 100.0,
),
),
),
I think you can also use InkWell apart from GestureDetector just wrap the card inside InkWell()
Widget我想你也可以用墨水瓶除了GestureDetector只是包装卡里面的
InkWell()
的Widget
InkWell(
onTap: (){ print("Card Clicked"); }
child: new Card(),
);
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.您可以使用 Inkwell 并插入 splashColor,在用户单击时,它会在卡片上创建具有所选颜色的回弹效果。这主要用于材料设计。
return Card(
color: item.completed ? Colors.white70 : Colors.white,
elevation: 8,
child: InkWell(
splashColor: "Insert color when user tap on card",
onTap: () async {
},
),
);
In Flutter, InkWell is a material widget that responds to touch action.在 Flutter 中,InkWell 是一个响应触摸动作的材质小部件。
InkWell(
child: Card(......),
onTap: () {
print("Click event on Container");
},
);
GestureDetector is a widget that detects the gestures. GestureDetector 是一个检测手势的小部件。
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. InkWell 是一个材质小部件,它可以在收到触摸时向您显示波纹效果。
GestureDetector is more general-purpose, not only for touch but also for other gestures. GestureDetector 更通用,不仅用于触摸,还用于其他手势。
The most preferred way is to add ListTile
as Card
child.最优选的方法是将
ListTile
添加为Card
子项。 Not only does ListTile
contain the method onTap
it also helps you in making Card interesting. ListTile
不仅包含onTap
方法,它还可以帮助您使 Card 变得有趣。
Card(
child: ListTile(
title: Text('Title')
leading: CircleAvatar(
backgroundImage: AssetImage('assets/images/test.jpg'),
),
onTap: () {
print('Card Clicked');
},
),
),
You also can insert a card into a TextButton:您还可以将卡片插入 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.
例如在 Flutter Web 中,你会得到一个鼠标悬停效果,光标会变成手,这样用户就知道,他可以点击那里。 Other additional features can be customised using the style.
可以使用样式自定义其他附加功能。
Do something on tap/click of 'child' in Flutter:-在 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
.第 1 步:- 将鼠标光标放在
Card
然后Alt+Enter
(在 Windows 中) select wrap with widget
。
Step2 :- Change your default widget into GestureDetector
.第 2
GestureDetector
:- 将您的默认小部件更改为GestureDetector
。
final code:-最终代码:-
child: GestureDetector(
onTap: YourOnClickCode(),
child: Card(------
------------
--------),
),
Wrap a card in GestureDetector Widget like a below:在 GestureDetector Widget 中包裹一张卡片,如下所示:
GestureDetector(
onTap: () {
// To do
},
child: Card(
),
),
Another way is as follows:另一种方式如下:
InkWell(
onTap: () {
// To do
},
child: Card(),
),
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.大多数答案都很精彩,但我只想与想要在卡片或列表磁贴的 Tap 上制作/显示连锁反应的人分享我的答案。
Card(
child: TextButton(
onPressed: ()=> ...,
child: ListTile(
title: Text('title'),
),
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.