Flutter InkWell 小部件未通过容器的渐变和颜色显示

Roh*_*han 6 containers widget gesturedetector dart flutter

编辑:我尝试将 包装ContainerMaterial小部件中并将颜色属性移动到Material小部件,但我将一堆ResourceCards水平放置ListView,,因此小部件中的颜色Material似乎只填充ResourceCard.

ResourceCard在 Flutter 中创建了自己的小部件。我用 a 包裹它来GestureDetector检测点击,但我想显示某种反馈或效果来通知用户它被点击了。GestureDetector我决定用小部件替换InkWell,但我没有通过容器的线性渐变和颜色看到飞溅效果,所以我只是将其恢复为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',
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

小智 12

最简单的解决方案是使用一个Material小部件作为 的父级InkWell并将其设置color为透明。必须InkWell仅在卡上设置(在您的示例中,GestureDetector在整个列上设置)。为了适合确切的形状,它与您的卡片(容器)InkWell相同borderRadius

这是您的构建方法的解决方案。我将InkWellMateral小部件放置为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,
                ),
              ),
            ),
          ),
        ),
        ...

Run Code Online (Sandbox Code Playgroud)

  • 如果我在容器上添加装饰,则对我不起作用 (6认同)
  • @Moralde-Sama 使用 Ink 小部件包装 InkWell,该小部件具有装饰参数 (6认同)