Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to blur a Container or Any widget in Flutter

Tags:

flutter

I am trying to achieve a blur effect on a Container widget something like this.

Expectation:

enter image description here

I tried to achieve it with BackdropFilter with ImageFilter.blur filter but it's not of any help.

Code

child: Container(
    child: Stack(
      children: <Widget>[
        BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
          child: Container(
            decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: color
            ),
            height: 60,
            width: 60,
          ),
        ),
        Positioned(
          left: 15,
          top: 15,
          child: Container(
            decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.lightBlue
            ),
            height: 30,
            width: 30,
          ),
        ),
      ]
    ),
  )

Output:

enter image description here

like image 229
ImMathan Avatar asked Dec 21 '18 14:12

ImMathan


People also ask

How do you create a widget blur in Flutter?

Add the Blur widget (inside the Column). Add the Container widget (inside the Blur) and set its width to infinity and height to 50. Also, make the container's background around 40% transparent by selecting the Fill Color and bringing the second slider to the left.

Is container a widget in Flutter?

Container class in flutter is a convenience widget that combines common painting, positioning, and sizing of widgets. A Container class can be used to store one or more widgets and position them on the screen according to our convenience. Basically, a container is like a box to store contents.


2 Answers

Container(
  height: 300,
  width: MediaQuery.of(context).size.width,
  decoration: BoxDecoration(
    image: DecorationImage(
      image: NetworkImage('your image url'),
      fit: BoxFit.cover
    ),
  ),
  child: ClipRect(
    child: BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 2.0, sigmaY: 2.0),
      child: Container(
        color: Colors.black.withOpacity(0.1),
      ),
    ),
  ),
);

The best way to make blur container in flutter and I am using clipRect to avoid whole screen blur.

like image 108
Rohit Sainik Avatar answered Oct 07 '22 17:10

Rohit Sainik


Widgets do not have a direct way to blur itself(as for as I know). But you can achieve it by using a CustomPainter.

MaskFilter.blur(BlurStyle.normal, blurSigma) can add the blur effect to any widget you want to draw yourself.

For example,

circle_blur_painter.dart

class CircleBlurPainter extends CustomPainter {

  CircleBlurPainter({@required this.circleWidth, this.blurSigma});

  double circleWidth;
  double blurSigma;

  @override
  void paint(Canvas canvas, Size size) {
    Paint line = new Paint()
      ..color = Colors.lightBlue
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = circleWidth
      ..maskFilter = MaskFilter.blur(BlurStyle.normal, blurSigma);
    Offset center = new Offset(size.width / 2, size.height / 2);
    double radius = min(size.width / 2, size.height / 2);
    canvas.drawCircle(center, radius, line);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

And you can use the CircleBlurPainter with a CustomPaint widget with foregroundPainter attribute.

blur_widget.dart

class BlurWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return CustomPaint(foregroundPainter: CircleBlurPainter(circleWidth: 30, blurSigma: 3.0));
  }
}

blurred widget output

like image 24
ImMathan Avatar answered Oct 07 '22 17:10

ImMathan