EDIT:我曾尝试将Container
包装在Material
小部件中,并将颜色属性移动到Material
小部件,但我将一堆ResourceCards
放置在水平的ListView,
中,因此Material
小部件的颜色似乎只填充了ResourceCard
周围的空间。
我在Flutter中创建了自己的小部件ResourceCard
。我用GestureDetector
来检测敲击,但我想显示某种反馈或效果来通知用户它被敲击了。我决定用InkWell
小部件替换GestureDetector
,但通过容器的线性渐变和颜色看不到飞溅效果,所以我只是将其恢复为GestureDetector
。我见过其他有可能解决问题的帖子,但没有一个能使用线性渐变和颜色。以下是其中一个ResourceCard
小部件的外观:https://i.stack.imgur.com/N2txv.jpg.这是小部件的代码:
class ResourceCard extends StatelessWidget {
ResourceCard({
@required this.colour,
@required this.margin,
@required this.cardText,
this.onPress,
@required this.cardCaption,
@required this.paddingText,
});
final Color colour;
final String cardText;
final Function onPress;
final EdgeInsets margin;
final String cardCaption;
final EdgeInsets paddingText;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Column(
children: <Widget>[
Container(
width: 75.0,
height: 75.0,
child: Center(
child: Text(
cardText,
style: TextStyle(
color: Colors.white,
fontFamily: 'Circular STD',
fontWeight: FontWeight.w900,
fontSize: 20.0,
),
),
),
margin: margin,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [colour, Colors.blue],
),
color: colour,
borderRadius: BorderRadius.circular(15.0),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(0, 0, 0, 0.5),
blurRadius: 10.0,
spreadRadius: 1.0,
)
],
),
),
Padding(
padding: paddingText,
child: Text(
cardCaption,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white70,
fontWeight: FontWeight.w300,
fontSize: 10.0,
fontFamily: 'Circular STD',
),
),
),
],
),
);
}
}
最简单的解决方案是使用Material
小部件作为InkWell
的父级,并将其color
设置为透明。InkWell
必须仅在卡上设置(在您的示例中,GestureDetector
设置在整列上(。为了适应确切的形状,InkWell
获得与您的卡(容器(相同的borderRadius
以下是您的构建方法的解决方案。我放置了InkWell
和Materal
小部件作为Center
小部件的父级
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
width: 75.0,
height: 75.0,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onPress,
borderRadius: BorderRadius.circular(15.0),
splashColor: Colors.grey[500],
child: Center(
child: Text(
cardText,
style: TextStyle(
color: Colors.white,
fontFamily: 'Circular STD',
fontWeight: FontWeight.w900,
fontSize: 20.0,
),
),
),
),
),
...
只需在Material
类上设置所需的背景颜色:
Material(
color: Colors.grey.shade200, // background color
child: InkWell(
onTap: () {},
splashColor: Colors.grey.shade300, // splash color
child: Container(
// content
),
),
);