gpt4 book ai didi

flutter - 如何在 flutter 中为 ClipOval 添加阴影?

转载 作者:行者123 更新时间:2023-12-03 13:29:42 30 4
gpt4 key购买 nike

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

所以,以下是我的代码:

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: () {},
),
),
),

],
),
),

以下是模拟:

mock

最佳答案

您可以创建自己的 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;
}
}

然后使用它
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: () {},
),
),
),
),

结果将是

enter image description here

关于flutter - 如何在 flutter 中为 ClipOval 添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690604/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com