如何向小部件边框/阴影添加霓虹灯发光效果?

ale*_*pfx 9 graphics dart dart-pub flutter flutter-layout

有没有办法使用颤振(带有特殊着色器的 CustomPaint 或类似的东西)来创建这样的效果?

在此处输入图片说明

在此处输入图片说明

例如。我有这个容器,我使用 CustomPainter 在它上面画了一些线。我可以像图片一样使用霓虹灯效果绘制这些线条吗?Paint 类有一个着色器属性,我认为我可以设置它来实现这个目标,但我不知道如何设置。

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*ndo 14

您可以使用 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,
                    ),
                  ),
                ]),
Run Code Online (Sandbox Code Playgroud)


Jah*_*tul 9

在Container小部件装饰中使用boxShadow属性两次。对于外发光,使用spreadRadius正值;对于内发光,使用负值。下面给出示例代码..

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),
      ),
    ),
  ),
Run Code Online (Sandbox Code Playgroud)