如何为小部件边框/阴影添加霓虹灯效果?
How to add a neon glow effect to an widget border / shadow?
有没有什么方法可以使用 flutter(带有特殊着色器的 CustomPaint 或类似的东西)来创建这样的效果?
例如。我有这个容器,我用 CustomPainter 在上面画了一些线。我可以像图片一样使用霓虹灯效果绘制这些线条吗? Paint class 有一个着色器 属性,我认为我可以设置它来实现这个目标,但我不知道如何实现。
Container(
color: Colors.white,
width: 300,
height: 300,
child: CustomPaint(
painter: NeonPainter(),
),
),
class NeonPainter extends CustomPainter {
Paint neonPaint = Paint();
NeonPainter() {
neonPaint.color = const Color(0xFF3F5BFA);
neonPaint.strokeWidth = 2.5;
neonPaint.shader = /// how to create a shader or something for that?
neonPaint.someOtherProperty///
}
@override
void paint(Canvas canvas, Size size) {
drawLine(canvas, size.width / 2 - 50, size.height / 2, size.width / 2 + 50,
size.height / 2);
drawLine(canvas, size.width / 2 + 50, size.height / 2, size.width / 2 + 100,
size.height / 2 + 50);
drawLine(canvas, size.width / 2 + 100, size.height / 2 + 50,
size.width / 2 - 100, size.height / 2 + 50);
drawLine(
canvas, size.width / 2 - 100, size.height / 2 + 50, size.width / 2 - 50,
size.height / 2);
}
void drawLine(canvas, double x1, double y1, double x2, double y2) {
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), neonPaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
您可以使用 BoxShadow 小部件..您可以设置颜色、blurRadius、SpreadRadius 和偏移来实现您想要的..
请注意,在示例中我已经使用它来获得阴影。但是如果正确设置属性,您可以获得发光效果。
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
boxShadow: [
BoxShadow(
color: Color(0xFF000000).withAlpha(60),
blurRadius: 6.0,
spreadRadius: 0.0,
offset: Offset(
0.0,
3.0,
),
),
]),
在 Container 小部件装饰中使用 boxShadow 属性 两次。对于外发光使用 spreadRadius positive 值,对于内发光使用 negetive 值。
示例代码如下..
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(18.0),
),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.pink,
spreadRadius: 4,
blurRadius: 10,
),
BoxShadow(
color: Colors.pink,
spreadRadius: -4,
blurRadius: 5,
)
]),
child: FlatButton(
onPressed:(){},
child: Text("submit"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
有没有什么方法可以使用 flutter(带有特殊着色器的 CustomPaint 或类似的东西)来创建这样的效果?
例如。我有这个容器,我用 CustomPainter 在上面画了一些线。我可以像图片一样使用霓虹灯效果绘制这些线条吗? Paint class 有一个着色器 属性,我认为我可以设置它来实现这个目标,但我不知道如何实现。
Container(
color: Colors.white,
width: 300,
height: 300,
child: CustomPaint(
painter: NeonPainter(),
),
),
class NeonPainter extends CustomPainter {
Paint neonPaint = Paint();
NeonPainter() {
neonPaint.color = const Color(0xFF3F5BFA);
neonPaint.strokeWidth = 2.5;
neonPaint.shader = /// how to create a shader or something for that?
neonPaint.someOtherProperty///
}
@override
void paint(Canvas canvas, Size size) {
drawLine(canvas, size.width / 2 - 50, size.height / 2, size.width / 2 + 50,
size.height / 2);
drawLine(canvas, size.width / 2 + 50, size.height / 2, size.width / 2 + 100,
size.height / 2 + 50);
drawLine(canvas, size.width / 2 + 100, size.height / 2 + 50,
size.width / 2 - 100, size.height / 2 + 50);
drawLine(
canvas, size.width / 2 - 100, size.height / 2 + 50, size.width / 2 - 50,
size.height / 2);
}
void drawLine(canvas, double x1, double y1, double x2, double y2) {
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), neonPaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
您可以使用 BoxShadow 小部件..您可以设置颜色、blurRadius、SpreadRadius 和偏移来实现您想要的..
请注意,在示例中我已经使用它来获得阴影。但是如果正确设置属性,您可以获得发光效果。
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
boxShadow: [
BoxShadow(
color: Color(0xFF000000).withAlpha(60),
blurRadius: 6.0,
spreadRadius: 0.0,
offset: Offset(
0.0,
3.0,
),
),
]),
在 Container 小部件装饰中使用 boxShadow 属性 两次。对于外发光使用 spreadRadius positive 值,对于内发光使用 negetive 值。 示例代码如下..
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(18.0),
),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.pink,
spreadRadius: 4,
blurRadius: 10,
),
BoxShadow(
color: Colors.pink,
spreadRadius: -4,
blurRadius: 5,
)
]),
child: FlatButton(
onPressed:(){},
child: Text("submit"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),