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

时间:2019-06-03 03:32:21

标签: graphics flutter dart flutter-layout dart-pub

是否可以使用颤动(带有特殊阴影的CustomPaint或类似的东西)创建类似效果的方法?

enter image description here

enter image description here

例如。我有这个容器,我使用CustomPainter在它上面画了一些线。我可以像图片一样使用霓虹灯效果画这些线吗? Paint类具有一个shader属性,我以为可以实现此目标,但是我不知道如何实现。

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;
  }
}

2 个答案:

答案 0 :(得分:1)

您可以使用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,
                    ),
                  ),
                ]),

答案 1 :(得分:0)

容器小部件装饰内

使用 boxShadow 属性两次。外部发光使用 spreadRadius 正值值,内部发光使用 negetive 值。 示例代码如下。

Container(
    decoration: BoxDecoration(
        borderRadius: BorderRadius.all(
          Radius.circular(18.0),
        ),
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.pink,
            spreadRadius: 4,
            blurRadius: 10,
            offset: Offset(0, 0),
          ),
           BoxShadow(
            color: Colors.pink,
            spreadRadius: -4,
            blurRadius: 5,
            offset: Offset(0, 0),
          )
        ]),
    child: FlatButton(
      onPressed:(){},
      child: Text("submit"),
      
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(18.0),
      ),
    ),
  ),