如何在颤动中为 ClipOval 添加阴影?

Gau*_*mar 6 flutter flutter-layout

作为初学者,我一直在尝试制作一个新的应用程序。所以,给事物添加阴影对我来说是全新的。

所以,以下是我的代码:

Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ClipOval(
                child: Material(
                  color: Colors.white, // button color
                  child: InkWell(
                    // splashColor: Colors.red, // inkwell color
                    child: SizedBox(
                        width: 46, height: 46, child: Icon(Icons.menu,color: Colors.red,),),
                    onTap: () {},
                  ),
                ),
              ),

            ],
          ),
        ),
Run Code Online (Sandbox Code Playgroud)

以下是模拟:

嘲笑

Omi*_*aha 7

向 ClipOval 添加阴影:

Center(
          child: Container(
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              boxShadow: [
                BoxShadow(
                  color: Colors.green,
                  blurRadius: 50.0,
                  spreadRadius: 10.0,
                )
              ],
            ),
            child: ClipOval(
              child: Image.network(
                'https://i.picsum.photos/id/384/536/354.jpg?hmac=MCKw0mm4RrI3IrF4QicN8divENQ0QthnQp9PVjCGblo',
                width: 100,
                height: 100,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述


laz*_*zos 5

您可以创建自己的CustomClipper

class CustomClipperOval extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromCircle(
        center: new Offset(size.width / 2, size.width / 2),
        radius: size.width / 2 + 3);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return false;
  }
}

class ClipOvalShadow extends StatelessWidget {
  final Shadow shadow;
  final CustomClipper<Rect> clipper;
  final Widget child;

  ClipOvalShadow({
    @required this.shadow,
    @required this.clipper,
    @required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _ClipOvalShadowPainter(
        clipper: this.clipper,
        shadow: this.shadow,
      ),
      child: ClipRect(child: child, clipper: this.clipper),
    );
  }
}

class _ClipOvalShadowPainter extends CustomPainter {
  final Shadow shadow;
  final CustomClipper<Rect> clipper;

  _ClipOvalShadowPainter({@required this.shadow, @required this.clipper});

  @override
  void paint(Canvas canvas, Size size) {
    var paint = shadow.toPaint();
    var clipRect = clipper.getClip(size).shift(Offset(0, 0));
    canvas.drawOval(clipRect, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后使用它

ClipOvalShadow(
  shadow: Shadow(
    color: Colors.amber,
    offset: Offset(1.0, 1.0),
    blurRadius: 2,
  ),
  clipper: CustomClipperOval(),
  child: ClipOval(
    child: Material(
      color: Colors.white, // button color
      child: InkWell(
        // splashColor: Colors.red, // inkwell color
        child: Container(
          width: 46,
          height: 46,
          child: Icon(
            Icons.menu,
            color: Colors.black,
          ),
        ),
        onTap: () {},
      ),
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

结果将是

在此处输入图片说明

  • 您的解决方案过于复杂:只需使用 `Material.type` 属性就足够了 - 请参阅原始问题下面的我的评论 (3认同)