在Flutter中实现内发光效果,可以通过以下几种方式:
1. 使用BoxDecoration(推荐)
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.blue.withOpacity(0.6), // 发光颜色
spreadRadius: -5, // 负值实现内发光
blurRadius: 20, // 模糊程度
offset: Offset(0, 0),
),
],
),
)
2. 使用Stack叠加实现
Stack(
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20),
),
),
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.blue.withOpacity(0.4),
spreadRadius: -10,
blurRadius: 15,
),
],
),
),
],
)
3. 自定义Painter实现更复杂效果
class InnerGlowPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..maskFilter = MaskFilter.blur(BlurStyle.inner, 10);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, size.width, size.height),
Radius.circular(20),
),
paint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
// 使用
CustomPaint(
size: Size(200, 200),
painter: InnerGlowPainter(),
)
关键参数说明:
- spreadRadius: 设为负值实现内发光效果
- blurRadius: 控制发光模糊程度
- color: 设置发光颜色和透明度
第一种方法最简单实用,能满足大部分内发光需求。